Combobox2列表取决于combobox1的选择

时间:2017-03-18 21:20:17

标签: vba excel-vba combobox excel

我有2个组合框位于userform1

可能的行源: sheet1 for food让我们说所有都在A行 Sheet2 for drinks

Combobox1有两个选择,例如“食物”,“饮料” 如果选择“food”,则自动填充sheet1中的rowsource 如果选择“drink”,则sheet2中的rowsource将自动填充

我不确定如何做到这一点。 我甚至没有示例代码或其他东西。

1 个答案:

答案 0 :(得分:0)

ComboBox1_Change事件中尝试此代码:

    Private Sub ComboBox1_Change()

        Dim Dsh As String, Fsh As String, Sh As Worksheet, Lr As Long
        Fsh = "Sheet1"
        Dsh = "Sheet2"

        Select Case Me.ComboBox1.Value
         Case "food"
           Set Sh = Sheets(Fsh)
         Case "drinks"
           Set Sh = Sheets(Dsh)
         Case Else
          Exit Sub
        End Select

        Lr = Sh.Range("A" & Sh.Rows.Count).End(xlUp).Row
        Me.ComboBox2.List = Sh.Range("A1:A" & Lr).Value

    End Sub