2016-05-13 12 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で別の方法があるようです。コマンドボックスにある「Assign Macro」はコンボボックスのコンテキストメニューにありません。狩り続けるだろう。 –

関連する問題