【问题标题】:Populating a list based on dropdown selection根据下拉选择填充列表
【发布时间】:2016-05-13 09:32:10
【问题描述】:

我正在尝试根据在下拉菜单中选择的路由代码填充端口列表。下拉列表在 BASE_RouteCode ('Schedule Tool'!$F$8) 范围内,路由代码存储在动态范围 RouteCodes (=Routes!$B$2:INDEX(Routes!$B$2:$B$27, COUNTA(Routes!$B$2:$B$27))) 中,端口列表存储在沿着 RoutePorts (=Routes!$B$2:INDEX(Routes!$B$2:$AZ$27, COUNTA(Routes!$B$2:$AZ$27))) 中每个路由代码的行。

目的是让 BASE_RouteCode 的每一次更改都会触发填充端口列表的子程序;目前我已经拼凑起来作为一个快速的尝试。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("BASE_RouteCode")
    Call PopulatePortList
End Sub

Sub PopulatePortList()

Dim iCol As Integer, iRow As Integer
If IsNumeric(WorksheetFunction.Match(Range("BASE_RouteCode").Value, Range("Routecodes"), 0)) Then
    iRow = WorksheetFunction.Match(Range("BASE_RouteCode").Value, Range("Routecodes"), 0) + 1

    ' Testing code
    MsgBox "Row number for route " & Range("BASE_RouteCode").Value & " is " & iRow
    Worksheets("Schedule Tool").Cells(8, 9).Value = iRow

    ' FOR ... WHILE loop (through iCol values) to populate list goes here

Else
    MsgBox "Please select a valid route code."
End If
End Sub

但是,当我更改下拉列表值时,会出现短暂的闪烁,但没有明显发生任何事情,也没有触发代码中的断点。

问号:

  • 我不确定 KeyCells 是否应该与 Target 相同;那 是从我在其他地方找到的示例中复制的,但似乎都没有 工作。
  • 如果我尝试手动运行 PopulatePortList,我会得到 1004 进入IF子句时出错。

我哪里错了?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    请查看以下(调整后的)代码,让我知道它是否适合您:

    Private Sub Worksheet_Change(ByVal Target As Range)
        'The following line makes sure that this event will only continue if
        '   "BASE_RouteCode" has been changed and not if ANY of the other
        '   cells on this sheet have been changed.
        If Intersect(Target, Range("BASE_RouteCode")) Is Nothing Then Exit Sub
        'Unless there is a global variable called "KeyCells" there is not need
        '   for the following two lines
        'Dim KeyCells As Range
        'Set KeyCells = Range("BASE_RouteCode")
    
        'The following line makes sure than any changes to the sheet
        '   (while the code is running) will not trigger another
        '   Worksheet change event. Otherwise, this will result in
        '   an endless loop and might crash Excel
        Application.EnableEvents = False
        Call PopulatePortList
        'Enable Events again before exiting. Otherwise this event will not work anymore.
        Application.EnableEvents = True
    End Sub
    
    Sub PopulatePortList()
    
    Dim iRow As Long
    Dim rngFound As Range
    
    Set rngFound = Worksheets("Routes").Range("Routecodes").Find(Worksheets("Schedule Tool").Range("BASE_RouteCode").Value, , xlValues, xlWhole)
    If Not rngFound Is Nothing Then
        iRow = rngFound.Row + 1
    
        ' Testing code
        MsgBox "Row number for route is " & rngFound.Row & ", " & _
            Chr(10) & "iRow is set to " & iRow & _
            Chr(10) & "and the value of BASE_RouteCode is " & rngFound.Value
        Worksheets("Schedule Tool").Cells(8, 9).Value = iRow
    
        ' FOR ... WHILE loop (through iCol values) to populate list goes here
    
    Else
        MsgBox "Please select a valid route code."
    End If
    
    End Sub
    

    我在代码中添加了一些 cmets 来解释我的更改。不过,如果您需要更多信息,请告诉我。

    【讨论】:

      【解决方案2】:

      我没有完全理解您的问题,但我认为您只是想在用户更改下拉选择时触发例程运行。

      如果是这种情况,那么我认为您不需要工作表更改事件。如果您只使用表单组合(开发人员功能区、控件组、插入,然后选择表单类别中的组合),您可以右键单击它并为其分配一个宏。当用户更改组合时,将触发此宏。通过右键单击并选择格式控件然后放入输入范围来填充此组合。您还可以指定将使用所选内容的索引填充的单元格(单元格链接)。

      【讨论】:

      • 目前我正在使用数据验证下拉菜单。我宁愿坚持下去,但我会尝试使用组合框并报告。
      • 试图弄清楚如何分配宏...似乎在当前版本的 Excel 中可能有不同的方法。在命令框中找到的“分配宏”不在组合框上下文菜单上。会继续打猎。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 2015-07-26
      • 1970-01-01
      相关资源
      最近更新 更多