【发布时间】:2017-03-30 20:34:40
【问题描述】:
我正在尝试避免使用循环来填充数组,因为它们在管理大量数据时会花费大量时间。
显然,这在 VBA 中是可能且容易的,但通常会导致问题。
代码如下:
sub populate()
'put the whole column in an array
Dim AppArray() As Variant
Dim AppRange As Range
'calculate the last row of the column 1 of sheets
Dim LstRow As Integer
LstRow = Sheets("whole").Cells(Sheets("whole").Rows.Count, "A").End(xlUp).row
'here I calculate the range that I want to pass to the array
Set AppRange = Sheets("whole").Range(Cells(1, 1), Cells(LstRow, 1))
MsgBox ("apprange " & AppRange.Address)
'i dont know if I need to redim or not
ReDim AppArray(1 To LstRow)
'here comes the point. populate the array with the values of the range
AppArray = AppRange.Value
End Sub
这不起作用。我也试过application.tranpose(AppRange.Value)。
我用过:
For i = 1 To LstRow
Debug.Print AppArray(i)
Next
然后出现错误,所以不知何故没有AppArray(1)。
如果您能对此发表评论,我将非常高兴。不仅仅是安排代码,甚至建议其他页面(链接)在事先不知道这些范围的情况下使用范围值填充数组。
如果情况是循环非常耗时并且可以立即填充数组,我不明白为什么 99% 引用数组的页面使用循环(或嵌套循环)来填充数组。
【问题讨论】:
-
你的问题真的很难理解,但循环的性能是你最不应该担心的事情。
标签: arrays excel vba performance populate