【发布时间】:2021-03-01 23:18:09
【问题描述】:
下面我有一个宏,它从一个范围形成一个数组,并将其发布到ActiveX ListBox。
代码有效,但不断发布到电子表格的速度很慢。有没有办法更有效地将整个数组发布到ListBox?
见下面的代码:
Sub Test()
'Lets format and populate the Listbox102
Dim p as integer, i as integer, j as integer, FinalCombinedArray as variant
FinalCombinedArray=ThisWorkbook.Sheets("Worksheet").Range("A1:D300")
'Keep track of where the scrollbar is currently
p = ThisWorkbook.Sheets("Worksheet").ListBox102.TopIndex
ThisWorkbook.Sheets("Worksheet").ListBox102.Clear
'Inset array element by element
For i = 1 To UBound(FinalCombinedArray, 2)
For j = 1 To UBound(FinalCombinedArray, 1)
If i = 1 Then
ThisWorkbook.Sheets("Worksheet").ListBox102.AddItem
End If
'Format array numbers
If ((j + 1) Mod 14) = 0 Then
ThisWorkbook.Sheets("Worksheet").ListBox102.List(j - 1, i - 1) = Format(FinalCombinedArray(j, i), "#,##0.0000")
Else
ThisWorkbook.Sheets("Worksheet").ListBox102.List(j - 1, i - 1) = Format(FinalCombinedArray(j, i), "#,##0.00")
End If
Next j
Next i
ThisWorkbook.Sheets("Worksheet").ListBox102.AddItem
'If possible, bring scrollbar back to where it was
If ThisWorkbook.Sheets("Worksheet").ListBox102.ListCount - 1 > p Then
ThisWorkbook.Sheets("Worksheet").ListBox102.TopIndex = p
End If
End Sub
【问题讨论】: