【问题标题】:Combo Box with switch case带开关盒的组合框
【发布时间】:2018-01-16 16:50:39
【问题描述】:

我正在尝试创建一个组合框,它将根据之前的组合框进行切换。所以我从第一个组合框创建了一个模块并将这段代码放在那里。第一个框按预期填充

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 而不是模块。

我的问题实际上是如何让组合框根据以前的组合框进行切换?

【问题讨论】:

  • 不确定您的问题。
  • 如何让一个组合框根据之前的组合框进行切换?

标签: excel combobox vba


【解决方案1】:

尝试以下方法,您就快到了:

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

【讨论】:

  • 所以现在当我从第一个下拉列表 (DropDown9) 中选择时,我收到一个错误“无法运行宏......”
  • 这些组合框是在表单中还是在工作表上,如果在工作表上是 ActiveX 控件还是表单控件?
  • 工作表上的表单控件。我试图避免将其用作表单。
猜你喜欢
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2019-03-10
  • 2018-05-05
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多