Search code examples
excelvbacombobox

excel show/hide group based on combobox selection


I need some help to figure out how to unhide/hide a group based on an activeX combo box selection.

I currently have two groups (group_1 and group_2) and a combobox (activeX) with two selections (2021-2022 and 2022-2023).

When 2021-2022 is selected from the drop down, I want group_1 to be unhidden (it is hidden by default).

When 2022-2023 is selected from the drop down, I want group_1 to be hidden and group_2 unhidden (it is hidden by default).

I am very new to VBA and have tried to put some code together for the first group and drop down selection option, but I have had no luck.

Private Sub ComboBox1_Change_2()
    Select Case ComboBox1.Text
        Case "2021-2022"
            With ActiveSheet.Shapes("group_1")
                If .Visible = False Then .Visible = True Else .Visible = False
End With
End If
End Sub

Is this something that can be done?


Solution

  • Try this:

    Private Sub ComboBox1_Change()
        Dim txt
        txt = ComboBox1.Text
        With Me 'assuming this is in the worksheet code module
            .Shapes("group_1").Visible = txt = "2021-2022"
            .Shapes("group_2").Visible = txt = "2022-2023"
            .Shapes("group_3").Visible = Len(txt) > 0 'any option selected
        End With
    End Sub