Populate a Textbox based on Combo box value.

Stusouth

New Member
Joined
Dec 5, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Sorry if this really basic.

I have a very simple user form with a combo box and a text box. I can populate the combo box with a list of codes from a range. What I'm trying then to do is populate the text box a the corresponding description. ie code 1234 is Cell A3 and the description is in cell B3.
This code sits in the user form

Dim xRg As Range

Private Sub UserForm_Initialize()
Set xRg = Worksheets("Sheet1").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

I can call the userform ok, but when I go and select the code in the Combo Box I get an runtime error on the Private Sub ComboBox1_Change(), 1004, unable to get the Vlookup property of the worksheetfuntion class. Please what I'm I doing wrong??
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try
VBA Code:
Private Sub ComboBox1_Change()
Me.TextBox1.Text = Sheets("Sheet1").Range("B" & Me.ComboBox1.ListIndex + 3)
End Sub
 
Upvote 0
try following update to code & see if helps

VBA Code:
Dim xRg As Range

Private Sub UserForm_Initialize()
    Set xRg = Worksheets("Sheet1").Range("A2:B8")
    Me.ComboBox1.List = xRg.Columns(1).Value
End Sub

Private Sub ComboBox1_Change()
    Me.TextBox1.Value = xRg.Cells(Me.ComboBox1.ListIndex + 1, 2).Value
End Sub

Dave
 
Upvote 0
Not sure which of you are referring to, but glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,820
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top