基于先前的组合框选择填充组合框

时间:2012-11-13 21:56:05

标签: vba excel-vba excel

我有两个组合框。第一个组合框有三个项目:{One,Tow,Three}现在我想根据用户在combobox1中选择的内容加载第二个组合框。 例如,如果用户在from combobox1中选择One,则namebox1将填充到combox2 如果用户在from combobox2中选择Two,则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是usinf excel集合选择框

1 个答案:

答案 0 :(得分: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的意思