Combobox取决于复选框

时间:2014-06-26 15:01:55

标签: forms excel vba checkbox combobox

我能够根据是否使用以下代码选择了Optionbox来更改Combobox的列表(或RowSource):

Private Sub optYes_Click()
    Options
End Sub

Private Sub optNo_Click()
    Options
End Sub

Private Sub Options()
    Select Case True
        Case optYes.Value = True
            cmb.Enabled = True
            cmb.RowSource = "=Options!A1:A4"
        Case optNo.Value = True
            cmb.Enabled = False
    End Select
End Sub

我想稍微修改一下,以便Combobox列表仅限于一组已选中的复选框。因此,如果我有10个复选框表示不同的选项,并且用户只选择其中的4个,那么只有那4个将出现在Combobox中。

1 个答案:

答案 0 :(得分:1)

我将如何做到这一点:

Private Sub Algeria_Change()
    Options
End Sub

Private Sub Bangladesh_Change()
    Options
End Sub

Private Sub Canada_Change()
    Options
End Sub

Private Sub Denmark_Change()
    Options
End Sub

Private Sub Options()
    Dim names As Variant, name As Variant
    Dim old As String

    names = Array("Algeria", "Bangladesh", "Canada", "Denmark")

    old = cmb

    cmb.Clear
    cmb.Enabled = False
    For Each name In names
        If Me.Controls(name) Then
            cmb.AddItem Me.Controls(name).Caption
            cmb.Enabled = True
            If name = old Then cmb.SelText = old
        End If
    Next name
End Sub

如果您需要更多复选框,只需将其姓名添加到names,然后在更改时调用Options