excelvbaif-statementcombobox

Excel VBA - how do I get the RowSource of a ComboBox to change depending on the value of another Combobox?


basically I'm trying to have two ComboBoxes that interact with each other in my UserForm. The first one having a simple list of maybe ten things as it's RowSource. These ten things however work as headlines for subsidiary lists (each containing maybe 15 things).

I want to be able to just show the respective 15 things in the second ComboBox, depending on what the user picked in the first box.

I have been googling for a while now and couldn't really find a solution. Copilot however gave me the following code, which makes sense to me but doesn't work because "Compile Error: Sub or Function not defined.".

Private Sub ComboBox1_Change()
    If Me.ComboBox1.Value = "Headline1" Then Me.ComboBox2.RowSource = Info!A2: A25
    End If
    
End Sub

Headline1 meaning an item from the list that provides the RowSource for Combobox 1. Info is the name of the sheet where all of this is on.

I'm using the latest version of Excel. (I'm new to VBA and unexperienced in coding either way btw.)

Thank you in advance!


Solution

  • Your RowSource needs to be properly formatted

    Me.ComboBox2.RowSource = "'Info'!$A$2:$A$25"
    

    or

    Me.ComboBox2.RowSource = "'[book1.xlsm]Info'!$A$2:$A$25"