ユーザーがさまざまな PLC モジュールの大きなリストから選択して PLC IO 構成をセットアップできるようにするアプリケーションを Excel で開発しています。モジュールは、製造元、デバイス ファミリ、デバイス タイプ、モジュール名の順に選択して選択します。各属性の選択は、異なる ListBox コントロールに対応します。各 ListBox の ListFillRange は、前の ListBox によって制御されます。たとえば、最初の ListBox をクリックして製造元を選択すると、VBA コードは前のデバイス ファミリ ListFillRange をクリアし、新しく選択した製造元のすべてのデバイス ファミリを再設定します。また、各 ListBox は ListFillRange として特定の範囲を使用します。VBA を使用して各 ListBox 選択リストをより直接的に設定できる場合もありますが、これを行う方法を理解できませんでした。各 ListBox は ListBox_Click() イベントを使用してそのコードを実行します。私が抱えている問題は次のとおりです。
1 つの ListBox が次の ListBox の ListFillRange をクリアすると、次の ListBox のコードが実行されるようになり、「実行時エラー '1004': Range クラスの Clear メソッドが失敗しました」というエラーが発生することがよくあります。
コードでエラーが発生しない場合は、次の ListBox のサイズが読み取れないポイントに変更されることがよくあります。
以下は、Device Family ListBox がクリックされたときに実行されるコードの例です。これにより、Module Types List Fill Range がクリアされ、新しく選択したデバイス ファミリのすべてのモジュール タイプが再入力されます。
Private Sub ListBox2_Click()
Dim row As Integer
row = 2
Dim totalRows As Integer
Dim ModuleTypes(30) As Variant
Dim ArrayIndex As Integer
ArrayIndex = 0
totalRows = ThisWorkbook.Sheets("Tables").Cells(2, 5).Value
'Clear the Module Type selection list on the PLC Module Data Sheet.
'ThisWorkbook.Sheets("PLC Module Data").Range("C2:C500").Clear
'Clear the Table Name search result list on the PLC Module Data Sheet.
ThisWorkbook.Sheets("PLC Module Data").Range("I2:L500").Clear
'Search the Table Name sheet for all entries fromm the selected MFG and with the selected Family and paste them onto the PLC Module Data Sheet.
For i = 2 To totalRows
If (ThisWorkbook.Sheets("Tables").Cells(i, 2).Value = ListBox1.Value) And (ThisWorkbook.Sheets("Tables").Cells(i, 3).Value = ListBox2.Value) Then
ThisWorkbook.Sheets("PLC Module Data").Cells(row, 9).Value = ThisWorkbook.Sheets("Tables").Cells(i, 1).Value
ThisWorkbook.Sheets("PLC Module Data").Cells(row, 10).Value = ThisWorkbook.Sheets("Tables").Cells(i, 2).Value
ThisWorkbook.Sheets("PLC Module Data").Cells(row, 11).Value = ThisWorkbook.Sheets("Tables").Cells(i, 3).Value
ThisWorkbook.Sheets("PLC Module Data").Cells(row, 12).Value = ThisWorkbook.Sheets("Tables").Cells(i, 4).Value
row = row + 1
End If
Next I
'Form an array of all unique Module Types within the Table search results list.
For i = 2 To ThisWorkbook.Sheets("PLC Module Data").Cells(2, 13).Value
If ArrayTest(ModuleTypes, ThisWorkbook.Sheets("PLC Module Data").Cells(i, 12).Value) Then
ModuleTypes(ArrayIndex) = ThisWorkbook.Sheets("PLC Module Data").Cells(i, 12).Value
ArrayIndex = ArrayIndex + 1
End If
Next i
'Copy the ModuleTypes Array into the Module Type selection list on the PLC Module Data Sheet.
For i = 0 To UBound(ModuleTypes)
ThisWorkbook.Sheets("PLC Module Data").Cells(2 + i, 3).Value = ModuleTypes(i)
Next i
'Clear the Temp search result list on the PLC Module Data Sheet.
ThisWorkbook.Sheets("PLC Module Data").Range("U2:X500").Clear
上記の問題を回避し、一連の ListBoxes をシームレスに連携させる方法を誰かが知っていれば、非常にありがたいです。ありがとう。