简体   繁体   English

动态数组的Excel VBA溢出错误

[英]Excel VBA Overflow Error with Dynamic Array

I'm getting an Overflow area in the following sub, and I can't figure out why. 我在下面的子区域中出现了一个“溢出”区域,我不知道为什么。 Stepping through the code, lRows and lCols gets set to the correct values, and the redims set the correct ranges on the arrays, but it fails when I try to assign the range values to the array (on line: arrData = rng.value). 逐步执行代码,将lRows和lCols设置为正确的值,并且redims在数组上设置了正确的范围,但是当我尝试将范围值分配给数组时,它失败了(在线:arrData = rng.value) 。 My rows do often go up to around 90,000+, but I have everything as long, so I would think that wouldn't be a problem... 我的行确实经常增加到90,000左右,但是我拥有的时间都足够长,所以我认为这不会成为问题...

Sub test()
Dim arrData() As Variant
Dim arrReturnData() As Variant
Dim rng As Excel.Range
Dim lRows As Long
Dim lCols As Long
Dim i As Long, j As Long

lRows = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
lCols = ActiveSheet.Range("A1").End(xlToRight).Column

ReDim arrData(1 To lRows, 1 To lCols)
ReDim arrReturnData(1 To lRows, 1 To lCols)
Set rng = ActiveSheet.Range(Cells(1, 1), Cells(lRows, lCols))
arrData = rng.value ' Overflow error on this line
For j = 1 To lCols
    For i = 1 To lRows
        arrReturnData(i, j) = Trim(arrData(i, j))
    Next i
Next j

rng.value = arrReturnData
End Sub

try 尝试

Dim arrData as Variant
arrData = Range(Cells(1, 1), Cells(lRows, lCols))

and for more info see this answer 有关更多信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM