How to populate textbox based on combobox selection on UserForm in Excel?
Supposing you have a table contains a Name and a Staff number column as below screenshot shown. Now you need to create a Userform embedded with a combo box which collecting all names inside and a text box which will populate corresponding staff number when selecting name from the combo box. The method in this article can help you to get it down.
Populate textbox based on combobox selection on Userform with VBA
Populate textbox based on combobox selection on Userform with VBA
Please do as follows to populate textbox with corresponding value based on combobox selection on Userform.
1. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, click Insert > Userform. See screenshot:
3. Then insert a Combo box control and a Text box control into the created Userform as below screenshot shown.
4. Right-click on any blank space of the Userform, and then click View Code from the context menu. See screenshot:
5. In the opening Userform (Code) window, please copy and paste the below VBA code to replace the original.
VBA code: Populate textbox based on combobox selection on Userform
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?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
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!











