简体   繁体   中英

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). 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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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