我在下面的 sub 中得到了一个溢出区域,我不知道为什么。逐步执行代码,lRows 和 lCols 被设置为正确的值,并且 redims 在数组上设置了正确的范围,但是当我尝试将范围值分配给数组时它失败了(在线:arrData = rng.value) . 我的行确实经常上升到大约 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