UserForm VLOOKUP result in textbox

H

Hendrik.Kleine

Hi, I have a userform with a Combobox with names.

Below this are some fields like

Date of birth:
Nationality:

This data is stored in a worksheet and a vlookup on the name selected in the
combobox should be able to retrieve data to populate the textboxes in the
userform. Also, I suppose there should be a button the user has to click
after selecting a name from the combobox.

Would anyone know how to do this vlookup in VBA and populate the textboxes?

Any help greatly appriciated!
 
T

Tom Ogilvy

Possibly something like this:

Private Sub Combobox1_Click()
Dim rng as Range, r as Range, res as Variant
With Worksheets("Data")
set rng = .Range(.cells(2,"B"),.Cells(2,"B").End(xldown))

res = Application.Match(combobox1.Value,rng,0)
if not iserror(res) then
r = rng(res)
Textbox1.Text = .Cells(r.row,"A")
Textbox2.Text = .Cells(r.Row,"C")
else
msgbox Combobox1.Value & " was not found"
end if
end With
End Sub

Change the "B" to reflect the column letter where the Names are located and
change "Data" to reflect the sheet containing the data table. Make other
changes as appropriate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top