0

我有两个组合框。第一个组合框有三个项目:{One, Tow, Three} 现在我想根据用户在 combobox1 中选择的内容加载第二个组合框。例如,如果用户在 from combobox1 中选择一个,则 namebox1 将填充到combox2,如果用户在 from combobox2 中选择两个,则 namebox2 将填充到combox2 等等。请告诉我如何在 VBA 中执行此操作?

谢谢

这是更新代码:

    Private Sub ComboBox1_Change()
     Me.ComboBox2.Clear
     Select Case Me.ComboBox1.Value
       Case "One"
        With Me.ComboBox2
            .RowSource = "nameBox1"
        End With
       Case "Two"
        With Me.ComboBox2
            .RowSource = "nameBox1"
        End With
       Case "Three"
        With Me.ComboBox2
             .RowSource = "nameBox1"
        End With
     End Select
    End Sub

请注意,我没有使用 .addItem 来填充 ComboBox1。它已通过相同的方法 .RowSource 填充,这是使用 excel 集合选择框

4

1 回答 1

1

最简单的方法是使用Select Case语句。假设你有

Private Sub ComboBox1_Change()

Me.ComboBox2.Clear

Select Case Me.ComboBox1.Value
    Case "One"
        ' If your data was information you wanted to put in yourself:
        With Me.ComboBox2
            .AddItem "Adam"
            .AddItem "Allen"
            .AddItem "Andy"
        End With
    Case "Two"
        ' If your data existed in a worksheet range
        ComboBox2.RowSource = MyRange.Address            
        End With
    End Select

End Sub

我希望这足以让您继续前进-否则,我有点困惑-您能解释一下您NameBox在问题中的意思吗

于 2012-11-13T22:35:18.020 回答