在Excel中,我正在尝试避免使用数组填充数组,因为它们在管理大量数据时会花费很多时间。
显然,在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)
如果您能对此发表评论,我将非常高兴。不只是安排代码,甚至建议oterh
页(链接)在事先不知道范围的情况下使用范围的值填充数组。如果情况是循环非常耗时并且可以立即填充数组,我也不明白为什么99%的引用数组的页面都使用循环(或嵌套循环)来填充数组。
PS:这是我的第一个条目,其中有一个关于stackoverflow的问题,这是我的指南,老师指南和教授的上个月。谢谢。
我找到了答案。
dim myRange as range
dim myArray() as variant
myRange = range(cells(2,3),cells(10,15))
redeem myArray(1 to 20,1 to 20)
myArray=myRange
使用变量和数组总是比使用单元格值要快得多。