我需要加载ComboBox3,其中包含来自ListView3的项目,这些项目在ComboBox1中按值动态更改。
是否可以做这样的事情?
Private Sub ComboBox1_Change()
Call filterlist
'This line is what I need to change. Not working in this way
UserForm1.ComboBox3.AddItem = ListView3.ListItems
End Sub
子过滤值:
Private Sub filterlist()
Dim item As ListItem
Dim i As Long
Dim ws As Worksheet
Dim sonsat As Long
Set ws = Sheets("data")
ListView3.ListItems.Clear
sonsat = ws.Cells(Rows.Count, 3).End(xlUp).Row + 1
For i = 2 To sonsat
If ws.Cells(i, 3).Value = ComboBox1.Text Then
Set item = ListView3.ListItems.Add(, , ws.Cells(i, 1))
item.ListSubItems.Add Text:=ws.Cells(i, 2)
item.ListSubItems.Add Text:=ws.Cells(i, 3)
End If
Next i
End Sub
答案 0 :(得分:2)
我不是说“恐怖变化”的意思。我只是在猜测,但请看下一个例子。如果您的数据结构与图像一样,并且您想在ComboBox1中加载A,B和C,并且在combo1中选择A之后您希望combo2填充101,1010,102等,请尝试以下代码。
Private Sub UserForm_Initialize()
Dim dU1 As Object, cU1 As Variant, iU1 As Long, lrU As Long
Dim i As Integer
Set dU1 = CreateObject("Scripting.Dictionary")
lrU = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
cU1 = Worksheets("Data").Range("A2:A" & lrU) 'Starts in second row. First row left for titles
For iU1 = 1 To UBound(cU1, 1)
dU1(cU1(iU1, 1)) = 1
Next iU1
'now dU1 has unique values from column A
For i = 0 To dU1.Count - 1
ComboBox1.AddItem dU1.Keys()(i) 'Load Combobox1 with unique values from Column A
Next
End Sub
Private Sub ComboBox1_Change()
Dim lLastRow As Long
Dim i As Integer
ComboBox2.Clear
lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lLastRow
If Worksheets("Data").Cells(i, 1) = ComboBox1.Text Then
ComboBox2.AddItem (Worksheets("Data").Cells(i, 2))
End If
Next
End Sub
答案 1 :(得分:1)
AddItem()方法一次只允许添加一个对象,看起来就像是一次传递整个ListView项。相反,您应该迭代ListView项目:
For Each item in ListView3.ListSubItems
ComboBox.AddItem(item.Text)
Next item