vlookups in userform - selections to populate other fields

Newport Count

Active Member
Joined
Oct 25, 2009
Messages
328
Hi

I am trying to to use vlookup on a userform so that when a value is entered in comboboxA, textbox1 is filled with data from another worksheet - is this possible?

many thanks :)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Yes but you can probably do it without a lookup.

How has the combobox been populated and how is it related to the data on the other worksheet?
 
Upvote 0
Thanks Norie,

the combobox (cboCompanyName) is populated from a list of names in another worksheet as follows:

Code:
Private Sub UserForm_Initialize()
 
    Dim cCompanyName As Range
 
    Dim ws As Worksheet
    Set ws = Worksheets("DropDowns")
 
    For Each cCompanyName In ws.Range("CompanyName")
    With Me.cboCompanyName
     .AddItem cCompanyName.Value
    End With
    Next cCompanyName

the above populates the dropdown for company name - then based on what is chosen in company name determines the next dropdown for company contact (cboContactName) as follows:

Code:
Private Sub cboCompanyName_Change()
    Dim rngCompany As Range
    Dim rngList As Range
    Dim strSelected As String
    Dim LastRow As Long
 
      If cboCompanyName.ListIndex <> -1 Then
 
           strSelected = cboCompanyName.Value
 
           LastRow = Worksheets("DropDowns").Range("A" & Rows.Count).End(xlUp).Row
 
           Set rngList = Worksheets("DropDowns").Range("A2:A" & LastRow)
           cboContactName.Clear
 
            For Each rngCompany In rngList
                 If rngCompany.Value = strSelected Then
                      cboContactName.AddItem rngCompany.Offset(, 1)
                 End If
           Next rngCompany
 
      End If


in "DropDowns" i have 4 columns - columnA displays the company name, columnB displays a customer contact name, columnC displays the contact telephone number and columnD displays the contact email address.

There can be duplicate values for company name in Column A but all other values are unique - the 4 columns are sorted alphabetically in order of company name, then contact name

I have 2 text boxes in my userform textboxTel & textboxEmail - i require these 2 textboxes to display the chosen contact name's telephone number and email address if it is present in "DropDowns"

many many thanks as always :)
 
Upvote 0
I have found the solution to this:

Code:
Private Sub cboContactName_Change()
    Dim ws  As Worksheet
    Dim tmp As Range
    
    Set ws = Worksheets("DropDowns")
    Set tmp = ws.Range("B2:D201")
    txtContactTel.Value = WorksheetFunction.VLookup(cboContactName.Value, tmp, 2, False)

End Sub

i tried to find similar posts and managed to work it out

Cheers!:biggrin:
 
Upvote 0
I was going to suggest using Find but looks like you've found the solution.:)
 
Upvote 0
Ok back again!

the form works how i would like it but if the form is reset then i get the following error:

Run-time error '1004':
Unable to get the VLookup property of the WorksheetFunction class

the same error appears when i switch to a different company name, or if there is no contact name that defaults (so one can be typed).

must be still trying to find data that is not there in the lookup

any ideas? thanks in advance :confused:
 
Upvote 0
What's probably happening is that when you do change the value returned by cboContactName.Value is "".

You can get round that with various methods, the simplest being to check that the value isn't "".
Code:
If cboContactName.Value <> "" And cboContactName.ListIndex <> -1 Then
 ... code for lookup
End If
 
Upvote 0
Another approach would be to load the VLookedUp values into cboContactName

Code:
cboContactName.Clear
cboContactName.ColumnCount = 3
cboContatctName.ColumnWidths = ";0;0"

For Each rngCompany In rngList
    If rngCompany.Value = strSelected Then
        With cboContactName
            .AddItem rngCompany.Offset(, 1)
            .List(.ListCount - 1, 1) = rngCompany.Offset(0,2)
            .List(.ListCount - 1, 2) = rngCompany.Offset(0,3)
        End With
    End If
Next rngCompany

Then selecting a contactname would work like
Code:
Private Sub cboContactName_Change()
    With cboContactName
        If .ListIndex <> - 1 Then
            txtContactTel.Text = .List(.ListIndex, 1)
            txtEMail.Text = .List(.ListIndex, 2)
        Else
            txtContactTel.Text = vbNullString
            txtEMail.Text = vbNullString
        End If
    End With
End Sub
 
Upvote 0
Mike

That's a great idea - I've never really thought of doing that with a userform in Excel but it's kind of the 'norm' to do in Access.

I was going to suggest something similar, add one extra column and populate it with the row of the contact.

That could still be added and might be useful for other things, eg editing contact details.
 
Upvote 0
Mike

That's a great idea - I've never really thought of doing that with a userform in Excel but it's kind of the 'norm' to do in Access.

I was going to suggest something similar, add one extra column and populate it with the row of the contact.

That could still be added and might be useful for other things, eg editing contact details.
For editiing back to the worksheet, I'd add another hidden column for rngCompany.Address(,,,True) to find the proper cells to edit.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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