从数组中填充listbox.list
x= range("A1:d100000").value For i = 1 To UBound(x, 1) Sk = x(i, 1) & "·" & x(i, 2) & "·" & x(i, 3) & "·" & x(i, 4) If InStr(x(i,4), "text") Then s = s & "~" & Sk Else End If Next i ListBox1.List = Split(Mid$(s, 2), "~")
代码把匹配结果放到(ActiveX)listbox.list 1列中
如何根据SK列数将匹配结果放到listbox.list中,如此
if x(i, 1) & "·" & x(i, 2) so put x(i,1) to listbox 1 column, x(i,2) to listbox 2 column if x(i, 1) & "·" & x(i, 2) & "·" & x(i, 3) so put x(i,1) to listbox 1 column, x(i,2) to listbox 2 column, x(i,3) to listbox 3 column
这个代码做得很好,但需要三倍的时间
q = Split(Mid$(s, 2), "~") With ListBox1 .Clear For Li = LBound(q) To UBound(q) q1 = Split(q(Li), "·") .ColumnCount = UBound(q1) + 1 .ColumnWidths = Replace(Space(UBound(q1)), " ", "200;") & 200 .AddItem q1(0), Li For Li2 = 1 To UBound(q1) .List(Li, Li2) = q1(Li2) Next Li2 Next Li ' Stop End With
无论如何加快这个代码?
当你读取Range中的.Value属性时,你会得到一个基于1的数组。 一个ActiveX Listbox的List属性需要一个基于0的数组。 .AddItem属性总是很慢,你应该只使用它,当你只有几个项目。 对于任何超过几个项目,将您的数据转换为基于0的数组,并使用List属性。
Sub FillLb() Dim vaRange As Variant Dim aList() As Variant Dim i As Long, j As Long vaRange = Sheet1.Range("A1:D100000") ReDim aList(0 To UBound(vaRange, 1) - 1, 0 To UBound(vaRange, 2) - 1) Sheet1.ListBox1.ColumnCount = UBound(vaRange, 2) For i = LBound(vaRange, 1) To UBound(vaRange, 1) For j = LBound(vaRange, 2) To UBound(vaRange, 2) aList(i - 1, j - 1) = vaRange(i, j) Next j Next i Sheet1.ListBox1.List = aList End Sub