0
votes

I have a userform with 6 activex comboboxes.

And I have the following code to populate 4 of the activex comboboboxes to make combobox 2 and combobox 4 dependent on combobox 1 and combobox 3. Combobox1 and combobox 3 uses a range as rowsource.

Län2 = Combobox3 Kommun2 = Combobox4

Private Sub Län2_Change()
Me.Kommun2 = ""
Select Case Me.Län2
    Case "Stockholms län"
        Me.Kommun2.RowSource = "Stockholms_län"

    Case "Västra götalands län"
        Me.Kommun2.RowSource = "Västra_götalands_län"

    Case "Skåne län"
        Me.Kommun2.RowSource = "Skåne_län"

    Case "Uppsala län"
        Me.Kommun2.RowSource = "Uppsala_län"

    Case "Södermanlands län"
        Me.Kommun2.RowSource = "Södermanlands_län"

    Case "Östergötlands län"
        Me.Kommun2.RowSource = "Östergötlands_län"

    Case "Jönköpings län"
        Me.Kommun2.RowSource = "Jönköpings_län"

    Case "Kronobergs län"
        Me.Kommun2.RowSource = "Kronobergs_län"

    Case "Kalmar län"
        Me.Kommun2.RowSource = "Kalmar_län"

    Case "Gotlands län"
        Me.Kommun2.RowSource = "Gotlands_län"

    Case "Blekinge län"
        Me.Kommun2.RowSource = "Blekinge_län"

    Case "Hallands län"
        Me.Kommun2.RowSource = "Hallands_län"

    Case "Värmlands län"
        Me.Kommun2.RowSource = "Värmlands_län"

    Case "Örebro län"
        Me.Kommun2.RowSource = "Örebro_län"

    Case "Västmanlands län"
        Me.Kommun2.RowSource = "Västmanlands_län"

    Case "Dalarnas län"
        Me.Kommun2.RowSource = "Dalarnas_län"

    Case "Gävleborgs län"
        Me.Kommun2.RowSource = "Gävleborgs_län"

    Case "Västernorrlands län"
        Me.Kommun2.RowSource = "Västernorrlands_län"

    Case "Jämtlands län"
        Me.Kommun2.RowSource = "Jämtlands_län"

    Case "Västerbottens län"
        Me.Kommun2.RowSource = "Västerbottens_län"

    Case "Norrbottens län"
        Me.Kommun2.RowSource = "Norrbottens_län"

End Select
End Sub

I want to change this code to work in a worksheet with listfillrange.

However, the code I have doesn't work.

Private Sub Län1_Change()
    Dim Stockholm As String, Skåne As String

    Stockholm = "Sheet2!B1:B3"
    Skåne = "Sheet2!C1:C3"

    Select Case Me.Län1.Value
        Case "Stockholms län": Me.Kommun1.ListFillRange = Stockholm
        Case "Skåne län": Me.Kommun1.ListFillRange = Skåne

    End Select

End Sub

How can I use ListFillRange to give me dependent comboboxes in a worksheet and not in a userform. ??

1

1 Answers

0
votes

The data source can be set by using the Rowsource. The Län2_Change method can be implemented with less lines by simply replacing a space with an underscore. See code below.

Private Sub Län2_Change()
    Me.Kommun2.RowSource = Replace(Me.Län2, " ", "_")
End Sub

Private Sub Län1_Change()
    Dim Stockholm As String, Skåne As String

    Stockholm = "Sheet2!B1:B3"
    Skåne = "Sheet2!C1:C3"

    Select Case Me.Län1.Value
        Case "Stockholms län": Me.Kommun1.Rowsource = Stockholm
        Case "Skåne län": Me.Kommun1.ListFillRange = Skåne
    End Select
End Sub