VBA - Populate listbox in userform based on combo box selection

Saber75

New Member
Joined
Jul 31, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all, long time lurker, first time posting. I'm new to VBA, but I've been able to build a couple of user forms for my company's benefits team. My form populates data using xlookup and populating textboxes based on the combo box selection is working well. However, I am having an issue populating a listbox based on the same combo box change. I've been able to do it in excel using index/match, but the formula is much too long and I'm not sure how to begin to convert to VBA. I'm thinking I can use xLookup here, but am not sure how to return an array vs. a single cell reference? Any guidance would be greatly appreciated.
 

Attachments

  • example1.png
    example1.png
    154 KB · Views: 36
  • example2.png
    example2.png
    108.5 KB · Views: 22

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Forgot to include my code, not sure how to edit the original post

VBA Code:
Private Sub ComboBoxMonth_Change()
If ComboBoxMonth.Value = "January" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AH:AH"))
ElseIf ComboBoxMonth.Value = "February" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AI:AI"))
ElseIf ComboBoxMonth.Value = "March" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AJ:AJ"))
ElseIf ComboBoxMonth.Value = "April" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AK:AK"))
ElseIf ComboBoxMonth.Value = "May" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AL:AL"))
ElseIf ComboBoxMonth.Value = "June" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AM:AM"))
ElseIf ComboBoxMonth.Value = "July" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AN:AN"))
ElseIf ComboBoxMonth.Value = "August" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AO:AO"))
ElseIf ComboBoxMonth.Value = "September" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AP:AP"))
ElseIf ComboBoxMonth.Value = "October" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AQ:AQ"))
ElseIf ComboBoxMonth.Value = "November" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AR:AR"))
ElseIf ComboBoxMonth.Value = "December" Then
    Me.TextBoxToggleDaysApproved.Value = Application.WorksheetFunction.XLookup(CDbl(Me.TextBoxAssocID.Value), Sheets("Toggle_Data").Range("A:A"), _
    Sheets("Toggle_Data").Range("AS:AS"))
ElseIf ComboBoxMonth.Value = "Select Month" Then
    TextBoxToggleDaysApproved.Value = "0"
    Exit Sub
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,764
Messages
6,174,364
Members
452,558
Latest member
jswan83

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