Làm cách nào để điền hộp văn bản dựa trên lựa chọn hộp kết hợp trên UserForm trong Excel?
Giả sử bạn có một bảng chứa cột Tên và số Nhân viên như hình minh họa bên dưới. Bây giờ bạn cần tạo Biểu mẫu người dùng được nhúng với một hộp tổ hợp thu thập tất cả các tên bên trong và một hộp văn bản sẽ điền số nhân viên tương ứng khi chọn tên từ hộp tổ hợp. Phương pháp trong bài viết này có thể giúp bạn gỡ bỏ nó.
Điền vào hộp văn bản dựa trên lựa chọn hộp kết hợp trên Biểu mẫu người dùng với VBA
Điền vào hộp văn bản dựa trên lựa chọn hộp kết hợp trên Biểu mẫu người dùng với VBA
Vui lòng làm như sau để điền vào hộp văn bản với giá trị tương ứng dựa trên lựa chọn hộp kết hợp trên Biểu mẫu người dùng.
1. Nhấn nút Khác + F11 phím để mở Microsoft Visual Basic cho các ứng dụng cửa sổ.
2. bên trong Microsoft Visual Basic cho các ứng dụng cửa sổ, nhấp Chèn > Biểu mẫu người dùng. Xem ảnh chụp màn hình:
3. Sau đó chèn điều khiển hộp Tổ hợp và điều khiển hộp văn bản vào Biểu mẫu người dùng đã tạo như hình minh họa bên dưới.
4. Bấm chuột phải vào bất kỳ khoảng trống nào của Biểu mẫu người dùng, rồi bấm Mã Chế độ xem từ menu ngữ cảnh. Xem ảnh chụp màn hình:
5. Trong cửa sổ Userform (Code) đang mở, vui lòng sao chép và dán mã VBA bên dưới để thay thế bản gốc.
Mã VBA: Điền vào hộp văn bản dựa trên lựa chọn hộp kết hợp trên Biểu mẫu người dùng
Dim xRg As Range
'Updated by Extendoffice 2018/1/30
Private Sub UserForm_Initialize()
Set xRg = Worksheets("Sheet5").Range("A2:B8")
Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub ComboBox1_Change()
Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, xRg, 2, False)
End Sub
Note: In the code, A2:B2 is the range contains the values you will populate separately in both the Userform combo box and text box. And Sheet5 is the name of the worksheet contains the data range. Please change them as you need.
6. Press the Alt + Q keys to exit the Microsoft Visual Basic for Applications window.
7. Click Developer > Insert > Command Button (ActiveX Control) to insert a command button into the worksheet.
8. Right-click the sheet tab and select View Code from the right-clicking menu. Then copy and paste VBA code into the code window.
VBA code: Show userform
Private Sub CommandButton1_Click()
'Updated by Extendoffice 2018/1/30
UserForm1.Show
End Sub
9. Turn off the Design Mode in the workbook.
Click the command button to open the specified userform. Then you can see all names are collected in the combo box. When selecting a name from the combo box, the corresponding staff number will be populated into the textbox automatically as below screenshot shown.

Related articles:
- How to populate a combo box with specified data on Workbook open?
- How to auto populate other cells when selecting values in Excel drop down list?
- How to auto populate other cells when selecting values in Excel drop down list?
- How to populate google search results to worksheet in Excel?
The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
- Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
- Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
- More than 300 powerful features. Supports Office / Excel 2007-2021 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Sort comments by
#25271
This comment was minimized by the moderator on the site
Report
0
0
#25272
This comment was minimized by the moderator on the site
Report
0
0
#25273
This comment was minimized by the moderator on the site
0
0
#25274
This comment was minimized by the moderator on the site
Report
0
0
#25275
This comment was minimized by the moderator on the site
0
0
#25276
This comment was minimized by the moderator on the site
0
0
#25277
This comment was minimized by the moderator on the site
0
0
#25278
This comment was minimized by the moderator on the site
Report
0
0
#25279
This comment was minimized by the moderator on the site
0
0
#31200
This comment was minimized by the moderator on the site
0
0
#35985
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet