Vba 基于下拉选择填充列表
我正在尝试根据下拉菜单中选择的路由代码填充端口列表。下拉列表位于范围BASE\u RouteCode(Vba 基于下拉选择填充列表,vba,excel,Vba,Excel,我正在尝试根据下拉菜单中选择的路由代码填充端口列表。下拉列表位于范围BASE\u RouteCode('Schedule Tool'!$F$8)中,路线代码存储在动态范围RouteCodes(=Routes!$B$2:INDEX(Routes!$B$2:$B$27,COUNTA(Routes!$B$2:$B$27))),端口列表沿路由报告中每个路由代码的行存储(=Routes!$B$2:INDEX(Routes!$B$2:$AZ$27,COUNTA(Routes!$B$2:$AZ$27))) 其
'Schedule Tool'!$F$8
)中,路线代码存储在动态范围RouteCodes(=Routes!$B$2:INDEX(Routes!$B$2:$B$27,COUNTA(Routes!$B$2:$B$27))
),端口列表沿路由报告中每个路由代码的行存储(=Routes!$B$2:INDEX(Routes!$B$2:$AZ$27,COUNTA(Routes!$B$2:$AZ$27))
)
其目的是让基本路径代码的每次更改触发填充端口列表的子节点;目前,我已经拼凑了这个作为一个快速的尝试
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
但是,当我更改下拉列表值时,会有一些东西短暂闪烁,但没有明显的变化,代码中的断点也不会被触发
问号:
- 我不确定关键单元是否应与目标相同;那个 是从我在别处找到的一个例子中复制出来的,但两个例子似乎都不是 工作
- 如果我尝试手动运行PopulatePortList,我会得到一个1004 输入IF子句时出错
我哪里出错了?我没有完全理解您的问题,但我认为您只是试图在用户更改下拉选择时触发一个例程来运行
如果是这种情况,那么我认为您不需要工作表更改事件。如果只使用“窗体”组合(开发人员功能区、控件组、插入,然后在“窗体”类别中选择该组合),则可以右键单击该组合并为其指定宏。当用户更改组合时,将触发此宏。通过右键单击并选择format control(格式控制),然后放入输入范围,可以填充此组合框。您还可以指定一个单元格,该单元格将填充所选内容的索引(单元格链接) 请查看以下(调整后的)代码,并让我知道这是否适用于您:
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
我在代码中添加了一些注释来解释我的更改。不过,如果您需要更多信息,请告诉我。目前我正在使用数据验证下拉列表。我更愿意坚持这样做,但我将尝试使用组合框并返回报告。尝试找出如何分配宏。。。在当前版本的Excel中,它可能有不同的方法。在命令框中找到的“分配宏”不在组合框上下文菜单中。我们会继续打猎。