Combo Box selection to populate Form

HDfatboy03

Board Regular
Joined
May 23, 2010
Messages
62
Hello

I am working on a form where when a selection is made from a combo box the next box is populated automatically from information entered earlier in a worksheet (GolferDB). Specifically when the last name is selected from a combo box the first name appears in the next combo box automatically.

Below is the code that I have currently. It is working with 2 combo boxes but the First name is not tied into the Last name selection. I feel it would be more user friendly if it would populate automatically.

I'm very new at this any help would be greatly appreciated.


Private Sub UserForm_Initialize()
Dim cPart As Range
Dim cLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("GolferDB")
For Each cLoc In ws.Range("LastName")
With Me.cboLocation
.AddItem cLoc.Value
End With
Next cLoc
For Each cPart In ws.Range("FirstName")
With Me.cbopart
.AddItem cPart.Value
.List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
End With
Next cPart
Me.cbopart.SetFocus
End Sub

Thanks you in advance.

Bob
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try Follow Code

Code:
Private Sub cboLocation_Change()

Dim MatchFirstName As Integer
Dim ws As Worksheet

Set ws = Worksheets("GolferDB")
On Error Resume Next
MatchFirstName = WorksheetFunction.Match(Me.cboLocation, ws.Range("LastName"), 0)
If Err = 0 Then
    Me.cbopart.Value = WorksheetFunction.Index(ws.Range("FirstName"), MatchFirstName)
Else:
    Me.cbopart.Value = ""
End If
On Error GoTo 0

End Sub

Private Sub UserForm_Initialize()

Dim cPart As Range
Dim cLoc As Range
Dim ws As Worksheet

Set ws = Worksheets("GolferDB")

For Each cLoc In ws.Range("LastName")
    With Me.cboLocation
        .AddItem cLoc.Value
    End With
Next cLoc

For Each cPart In ws.Range("FirstName")
    With Me.cbopart
        .AddItem cPart.Value
        .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
    End With
Next cPart

Me.cbopart.SetFocus

End Sub

Sample Data
Excel Workbook
AB
1LastNameFirstName
2ABCC
3CDDD
4EFEE
5GHFF
6IJGG
7KLHH
8MNII
9OPJJ
GolferDB
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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