Excel VBAのユーザーフォームについて質問です。前回の質問で答えていただいた以下の構文で、さらにリストボックスの連動を自動で切り替えたいです。(詳細は画像になります)ご教授よろしくお願いいたします。Option Explicit' --- ListBox のペアを配列で管理 ---Private CompanyLists As Variant ' 社名 ListBox 配列Private ProductLists As Variant ' 商品 ListBox 配列Private Sub UserForm_Initialize()Dim i As Long'=== ListBox名を配列に格納 ===CompanyLists = Array(\u0026quot;ListBox1\u0026quot;, \u0026quot;ListBox3\u0026quot;, \u0026quot;ListBox5\u0026quot;, \u0026quot;ListBox7\u0026quot;, \u0026quot;ListBox9\u0026quot;)ProductLists = Array(\u0026quot;ListBox2\u0026quot;, \u0026quot;ListBox4\u0026quot;, \u0026quot;ListBox6\u0026quot;, \u0026quot;ListBox8\u0026quot;, \u0026quot;ListBox10\u0026quot;)'=== すべての社名ListBoxに社名一覧をセット ===For i = LBound(CompanyLists) To UBound(CompanyLists)LoadCompanyList Me.Controls(CompanyLists(i))Next iEnd SubPrivate Sub LoadCompanyList(lb As MSForms.ListBox)Dim lo As ListObject, dic As ObjectDim rng As Range, r As RangeSet lo = Sheet1.ListObjects(\u0026quot;テーブル1\u0026quot;)Set rng = lo.ListColumns(\u0026quot;社名\u0026quot;).DataBodyRangeSet dic = CreateObject(\u0026quot;Scripting.Dictionary\u0026quot;)lb.ClearFor Each r In rngIf Not dic.Exists(r.Value) Then dic.Add r.Value, r.ValueNext rlb.List = dic.ItemsEnd SubPrivate Sub FilterProduct(company As String, resultBox As MSForms.ListBox)Dim lo As ListObjectDim cmp As RangeDim prd As RangeDim i As LongSet lo = Sheet1.ListObjects(\u0026quot;テーブル1\u0026quot;)Set cmp = lo.ListColumns(\u0026quot;社名\u0026quot;).DataBodyRangeSet prd = lo.ListColumns(\u0026quot;商品名\u0026quot;).DataBodyRangeresultBox.ClearFor i = 1 To cmp.Rows.CountIf cmp.Cells(i, 1).Value = company ThenresultBox.AddItem prd.Cells(i, 1).ValueEnd IfNext iEnd SubPrivate Sub ListBox_Change(Index As Integer)If Me.Controls(CompanyLists(Index)).ListIndex = -1 Then Exit SubDim selectedCompany As StringselectedCompany = Me.Controls(CompanyLists(Index)).ValueFilterProduct selectedCompany, Me.Controls(ProductLists(Index))End SubPrivate Sub ListBox1_Change(): ListBox_Change 0: End SubPrivate Sub ListBox3_Change(): ListBox_Change 1: End SubPrivate Sub ListBox5_Change(): ListBox_Change 2: End SubPrivate Sub ListBox7_Change(): ListBox_Change 3: End SubPrivate Sub ListBox9_Change(): ListBox_Change 4: End Sub

Excel

1件の回答

回答を書く

1134871

2026-03-19 01:25

+ フォロー

連動しないのは、高さ不足だと思います。画像の様にスクロールできる高さにすれば、連動します。選択する会社をクリックして、青にすれば、右にその会社のリストが表示されます。

クラスモジュール使って、短くしました。



ユーザーフォーム

Option Explicit

Private colEvent As New Collection

'

Private Sub UserForm_Initialize()

    Dim Class As Class1

    Dim CNo As Integer

    Dim RInp As Long

    Dim NewItem As String

    Dim OldItem As String

'

    For CNo = 1 To 9 Step 2

        Set Class = New Class1

        Set Class.ListBox = Controls(\u0026quot;Listbox\u0026quot; \u0026amp; CNo)

        colEvent.Add Class

'

        For RInp = 2 To Cells(Rows.Count, \u0026quot;A\u0026quot;).End(xlUp).Row

            NewItem = Cells(RInp, \u0026quot;A\u0026quot;)

'

            If NewItem \u0026lt;\u0026gt; OldItem Then

                Controls(\u0026quot;Listbox\u0026quot; \u0026amp; CNo).AddItem NewItem

            End If

            OldItem = NewItem

    Next RInp, CNo

End Sub



クラスモジュール(Class1)

Option Explicit

Private WithEvents pListBox As MSForms.ListBox

Private pForm As MSForms.UserForm

'

Public Property Set ListBox(ByVal aListBox As MSForms.ListBox)

    Set pListBox = aListBox

    Set pForm = aListBox.Parent

End Property

'

Private Sub pListBox_Change()

    Dim CName As String

    Dim RSta As Long

'

    CName = \u0026quot;ListBox\u0026quot; \u0026amp; Mid(pListBox.Name, 8) + 1

    RSta = WorksheetFunction.Match(pListBox.Value, [A:A], 0)

    UserForm1.Controls(CName).RowSource = \u0026quot;B\u0026quot; \u0026amp; RSta \u0026amp; _

        \u0026quot;:B\u0026quot; \u0026amp; WorksheetFunction.CountIf([A:A], pListBox.Value) + RSta - 1

End Sub

うったえる有益だ(0シェアするブックマークする

Copyright © 2026 AQ188.com All Rights Reserved.

博識 著作権所有