2016-05-13 117 views
1

我试图根据在下拉菜单中选择的路由代码填充端口列表。下拉范围为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应该是一样的目标;那 是从我在其他地方找到的一个例子中复制出来的,但似乎并没有 的工作。
  • 如果我尝试手动运行PopulatePortList,那么当它进入IF子句时,会出现1004 错误。

我在哪里错了?

回答

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 

我加入到代码中的一些注释来解释我的变化。不过,让我知道你是否需要进一步的信息。

+0

工作完美 - 非常感谢! –

1

我不完全按照你的问题,但我认为你只是试图触发例程在用户更改下拉选择时运行。

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

+0

目前我正在使用数据验证下拉菜单。我宁愿坚持,但我会尝试使用组合框并回报。 –

+0

试图找出如何分配宏...似乎它可能在当前版本的Excel中有不同的方法。在命令框中找到的“分配宏”不在组合框上下文菜单中。将继续狩猎。 –

相关问题