VBA 在基于两个动态组合框的列表框中显示
VBA Display in a listbox based on two dynamic comboboxes
我想在列表框中显示 2 个组合框的结果,但我不知道该怎么做...
这是我目前得到的:
根据左侧组合框,它暗示右侧组合框的结果。
1/ 我想显示软件包版本 ($E) 和软件包版本 ($F)名称 selected 在项目的右侧组合框 selected 在左侧组合框,("monitoring" 在这个例子中)。
2/如果我select"ALL"包名,我想显示$E和$F.
这是我当前的代码:
Private Sub CommanButton1_Click()
Unload UserForm1
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell As Range, Key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("system-packages-installed")
UserForm1.ComboBox1.Clear
For Each rCell In ws.Range("B2", ws.Cells(Rows.Count, "B").End(xlUp))
If Not Dic.exists(LCase(rCell.Value)) Then
Dic.Add LCase(rCell.Value), Nothing
End If
Next rCell
UserForm1.ComboBox1.AddItem "ALL"
For Each Key In Dic
UserForm1.ComboBox1.AddItem Key
Next
End Sub
Private Sub ComboBox1_Click()
Dim rCell As Range, Key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("system-packages-installed")
UserForm1.ComboBox2.Clear
For Each rCell In ws.Range("B2", ws.Cells(Rows.Count, "B").End(xlUp))
If rCell.Value = ComboBox1.Value Then
If Not Dic.exists(LCase(rCell.Offset(, 1).Value)) Then
Dic.Add LCase(rCell.Offset(, 1).Value), Nothing
End If
End If
Next rCell
UserForm1.ComboBox2.AddItem "ALL"
For Each Key In Dic
UserForm1.ComboBox2.AddItem Key
Next
End Sub
Private Sub ComboBox2_Click()
'This is a test'
With UserForm1.ListBox1
.ColumnCount = 27
.ColumnWidths = "50"
.RowSource = "'system-packages-installed'!E:E"
End With
End Sub
如果您有任何问题,请随时问我。
提前致谢。
编辑:
这是 table :
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-0pky">System_id<br></th>
<th class="tg-0pky">Machine</th>
<th class="tg-0pky">package_name</th>
<th class="tg-0pky">package_epoch</th>
<th class="tg-0pky">package_version</th>
<th class="tg-0pky">package_release</th>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">acl</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">2.2.51</td>
<td class="tg-0pky">14.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">alsa-lib</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">1.1.6</td>
<td class="tg-0pky">2.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">apg</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">2.3.0b</td>
<td class="tg-0pky">24.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">apr</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">1.4.8</td>
<td class="tg-0pky">3.el7_4.1</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">apr-util</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">1.5.2</td>
<td class="tg-0pky">6.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">at</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">3.1.13</td>
<td class="tg-0pky">24.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">audit</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">2.8.4</td>
<td class="tg-0pky">4.el7</td>
</tr>
</table>
我的规则是永远不要使用 RowSource
。通常,如果您可以将信息放在一个数组中,那么您可以将该数组分配给 Lisbtobx.List
属性。另一种方法是使用 AddItem
添加新项目,然后使用 List()
属性 填写其余列。下面是一个例子。我对您现有的代码做了一些小改动,我认为 ComboBox2_Change 代码是您感兴趣的。
Private Sub ComboBox1_Change()
Dim rCell As Range, ws As Worksheet
Dim dc As Scripting.Dictionary
Set dc = New Scripting.Dictionary
dc.Add "ALL", "ALL"
Set ws = Worksheets("system-packages-installed")
Me.ComboBox2.Clear
For Each rCell In ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)).Cells
If rCell.Value = Me.ComboBox1.Value Then
If Not dc.exists(LCase(rCell.Offset(0, 1).Value)) Then
dc.Add LCase(rCell.Offset(0, 1).Value), LCase(rCell.Offset(0, 1).Value)
End If
End If
Next rCell
Me.ComboBox2.List = dc.Items
End Sub
Private Sub ComboBox2_Change()
Dim rCell As Range
Dim ws As Worksheet
Set ws = Worksheets("system-packages-installed")
Me.ListBox1.Clear
For Each rCell In ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)).Cells
If LCase(rCell.Value) = LCase(Me.ComboBox1.Value) Or Me.ComboBox1.Value = "ALL" Then
If LCase(rCell.Offset(0, 1).Value) = LCase(Me.ComboBox2.Value) Or Me.ComboBox2.Value = "ALL" Then
With Me.ListBox1
.AddItem rCell.Value 'column 1
.List(.ListCount - 1, 1) = rCell.Offset(0, 1).Value 'column 2
.List(.ListCount - 1, 2) = rCell.Offset(0, 3).Value 'column 3
.List(.ListCount - 1, 3) = rCell.Offset(0, 4).Value 'column 4
End With
End If
End If
Next rCell
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell As Range
Dim dc As Scripting.Dictionary
Set dc = New Scripting.Dictionary
dc.Add "ALL", "ALL"
Set ws = Worksheets("system-packages-installed")
Me.ComboBox1.Clear
For Each rCell In ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)).Cells
If Not dc.exists(LCase(rCell.Value)) Then
dc.Add LCase(rCell.Value), LCase(rCell.Value)
End If
Next rCell
Me.ComboBox1.List = dc.Items
End Sub
我想在列表框中显示 2 个组合框的结果,但我不知道该怎么做...
这是我目前得到的:
1/ 我想显示软件包版本 ($E) 和软件包版本 ($F)名称 selected 在项目的右侧组合框 selected 在左侧组合框,("monitoring" 在这个例子中)。
2/如果我select"ALL"包名,我想显示$E和$F.
这是我当前的代码:
Private Sub CommanButton1_Click()
Unload UserForm1
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell As Range, Key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("system-packages-installed")
UserForm1.ComboBox1.Clear
For Each rCell In ws.Range("B2", ws.Cells(Rows.Count, "B").End(xlUp))
If Not Dic.exists(LCase(rCell.Value)) Then
Dic.Add LCase(rCell.Value), Nothing
End If
Next rCell
UserForm1.ComboBox1.AddItem "ALL"
For Each Key In Dic
UserForm1.ComboBox1.AddItem Key
Next
End Sub
Private Sub ComboBox1_Click()
Dim rCell As Range, Key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("system-packages-installed")
UserForm1.ComboBox2.Clear
For Each rCell In ws.Range("B2", ws.Cells(Rows.Count, "B").End(xlUp))
If rCell.Value = ComboBox1.Value Then
If Not Dic.exists(LCase(rCell.Offset(, 1).Value)) Then
Dic.Add LCase(rCell.Offset(, 1).Value), Nothing
End If
End If
Next rCell
UserForm1.ComboBox2.AddItem "ALL"
For Each Key In Dic
UserForm1.ComboBox2.AddItem Key
Next
End Sub
Private Sub ComboBox2_Click()
'This is a test'
With UserForm1.ListBox1
.ColumnCount = 27
.ColumnWidths = "50"
.RowSource = "'system-packages-installed'!E:E"
End With
End Sub
如果您有任何问题,请随时问我。
提前致谢。
编辑:
这是 table :
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-0pky">System_id<br></th>
<th class="tg-0pky">Machine</th>
<th class="tg-0pky">package_name</th>
<th class="tg-0pky">package_epoch</th>
<th class="tg-0pky">package_version</th>
<th class="tg-0pky">package_release</th>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">acl</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">2.2.51</td>
<td class="tg-0pky">14.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">alsa-lib</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">1.1.6</td>
<td class="tg-0pky">2.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">apg</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">2.3.0b</td>
<td class="tg-0pky">24.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">apr</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">1.4.8</td>
<td class="tg-0pky">3.el7_4.1</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">apr-util</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">1.5.2</td>
<td class="tg-0pky">6.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">at</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">3.1.13</td>
<td class="tg-0pky">24.el7</td>
</tr>
<tr>
<td class="tg-0pky">1000010000</td>
<td class="tg-0pky">monitoring</td>
<td class="tg-0pky">audit</td>
<td class="tg-0pky"></td>
<td class="tg-0pky">2.8.4</td>
<td class="tg-0pky">4.el7</td>
</tr>
</table>
我的规则是永远不要使用 RowSource
。通常,如果您可以将信息放在一个数组中,那么您可以将该数组分配给 Lisbtobx.List
属性。另一种方法是使用 AddItem
添加新项目,然后使用 List()
属性 填写其余列。下面是一个例子。我对您现有的代码做了一些小改动,我认为 ComboBox2_Change 代码是您感兴趣的。
Private Sub ComboBox1_Change()
Dim rCell As Range, ws As Worksheet
Dim dc As Scripting.Dictionary
Set dc = New Scripting.Dictionary
dc.Add "ALL", "ALL"
Set ws = Worksheets("system-packages-installed")
Me.ComboBox2.Clear
For Each rCell In ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)).Cells
If rCell.Value = Me.ComboBox1.Value Then
If Not dc.exists(LCase(rCell.Offset(0, 1).Value)) Then
dc.Add LCase(rCell.Offset(0, 1).Value), LCase(rCell.Offset(0, 1).Value)
End If
End If
Next rCell
Me.ComboBox2.List = dc.Items
End Sub
Private Sub ComboBox2_Change()
Dim rCell As Range
Dim ws As Worksheet
Set ws = Worksheets("system-packages-installed")
Me.ListBox1.Clear
For Each rCell In ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)).Cells
If LCase(rCell.Value) = LCase(Me.ComboBox1.Value) Or Me.ComboBox1.Value = "ALL" Then
If LCase(rCell.Offset(0, 1).Value) = LCase(Me.ComboBox2.Value) Or Me.ComboBox2.Value = "ALL" Then
With Me.ListBox1
.AddItem rCell.Value 'column 1
.List(.ListCount - 1, 1) = rCell.Offset(0, 1).Value 'column 2
.List(.ListCount - 1, 2) = rCell.Offset(0, 3).Value 'column 3
.List(.ListCount - 1, 3) = rCell.Offset(0, 4).Value 'column 4
End With
End If
End If
Next rCell
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet, rCell As Range
Dim dc As Scripting.Dictionary
Set dc = New Scripting.Dictionary
dc.Add "ALL", "ALL"
Set ws = Worksheets("system-packages-installed")
Me.ComboBox1.Clear
For Each rCell In ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)).Cells
If Not dc.exists(LCase(rCell.Value)) Then
dc.Add LCase(rCell.Value), LCase(rCell.Value)
End If
Next rCell
Me.ComboBox1.List = dc.Items
End Sub