带开关盒的组合盒

时间:2018-01-16 16:50:39

标签: excel-vba combobox vba excel

我正在尝试创建一个基于以前的组合框切换的组合框。所以我从第一个组合框创建了一个模块,并将此代码放在那里。第一个框按预期填充

Sub DropDown9_Change()
DropDown14 = ""

Select Case DropDown9
    Case "PIP"
        DropDown14.RowSource = "rsm"
    Case "SAFETY WORKS"
        DropDown14.RowSource = "rsm2"
    Case "ASI"
        DropDown14.RowSource = "rsm"
    Case "Century Glove"
        DropDown14.RowSource = "rsm"

End Select
End Sub

我认为这只适用于UserForm,而不适用于模块。

我的问题是如何根据之前的组合框进行组合框切换?

1 个答案:

答案 0 :(得分:0)

尝试以下内容,你几乎就在那里:

Private Sub DropDown9_Change()
DropDown14.Value = ""
    Select Case DropDown9.Value
        Case "PIP"
            DropDown14.Value = "rsm"
        Case "SAFETY WORKS"
            DropDown14.Value = "rsm2"
        Case "ASI"
            DropDown14.Value = "rsm"
        Case "Century Glove"
            DropDown14.Value = "rsm"
    End Select
End Sub

<强>更新

以下内容应该完成工作表上的表单控件下拉框:

Sub DropDown9_Change()
Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
Dim CheckDrop As Shape: Set CheckDrop = ws.Shapes("Drop Down 9")
Dim DestinationDrop As Shape: Set DestinationDrop = ws.Shapes("Drop Down 14")
'above declare and set your Sheet and the name of your drop downs
'as long as the Drop Down's have the values in their lists, then the Case below will select the appropriate value
With CheckDrop
        listNew = DestinationDrop.ControlFormat.List
        Select Case .ControlFormat.List(.ControlFormat.ListIndex)
            Case "PIP"

                If UBound(listNew) > 0 Then
                    For i = 1 To UBound(listNew)
                        If listNew(i) = "rsm" Then DestinationDrop.ControlFormat.ListIndex = i
                    Next i
                End If
            Case "SAFETY WORKS"

                If UBound(listNew) > 0 Then
                    For i = 1 To UBound(listNew)
                        If listNew(i) = "rsm2" Then DestinationDrop.ControlFormat.ListIndex = i
                    Next i
                End If
            Case "ASI"

                If UBound(listNew) > 0 Then
                    For i = 1 To UBound(listNew)
                        If listNew(i) = "rsm" Then DestinationDrop.ControlFormat.ListIndex = i
                    Next i
                End If
            Case "Century Glove"

                If UBound(listNew) > 0 Then
                    For i = 1 To UBound(listNew)
                        If listNew(i) = "rsm" Then DestinationDrop.ControlFormat.ListIndex = i
                    Next i
                End If
        End Select
End With
End Sub