1

The Task

I'm working on a VBA project wherein I store the data from a sheet in a Variant Array, do a bunch of calculations with the data in the Array, store the results of those computations in a few other Arrays, then finally I store all the "results arrays" in another Variant Array and write that array to a range in a new sheet.

Note: this process has been working fine ever since I first wrote the procedure.

The Issue

After a very minor logic change, my final Array will no longer write to the specified range. I get Run-time error '7': Out of memory when I try. I changed the logic back in case that was my issue, but I still get the error.

After doing some research, I tried to erase all the arrays (including the one containing all the sheet data) before writing to the range, but that didn't work either. I've checked Task Manager and that shows my RAM as having a lot of free space (almost 6K MBs available and over 1k Mbs "free"). (Task Manager shows the Excel process as using around 120 MBs.)

The Code (Snippet)

Dim R As Range  
Dim ALCount As Long  
Dim All(5) As Variant  
Dim sht As Worksheet
Dim Arr1() As Long
Dim Arr2() As Date
Dim Arr3() As Date
Dim Arr4() As Long
Dim Arr5() As String  

All(1) = Arr1
All(2) = Arr2
All(3) = Arr3
All(4) = Arr4
All(5) = Arr5      

Set R = sht.Cells(2,1)  
R.Resize(ALCount - 1, 5).Value = Application.Transpose(All)

More Details

My device: Win 7 64-Bit  
Excel: 2010 32-Bit  
File Size: <20 MBs  
Macro Location: Personal Workbook  
Personal WB Size: < 3 MBs  

Edit: Array Size: 773x5

Any thoughts on why this might be happening?

4

1 回答 1

2

我弄清楚了这个问题。

Arr5(一个字符串数组)是罪魁祸首。该数组中的每个元素都设置如下:

StrVariable = StrVariable & "--" & StrVariable    
Arr5(ALCount) = StrVariable

StrVariable我忘记在每个循环后包含此行以重置:

 StrVariable = vbNullString

结果意味着通过 773 次迭代,Arr5(773) 的字符串长度很大……事实上,即使我的代码可以处理数组,工作表也不能。

我通过在迭代 200 处停止并检查工作All表中的输出来发现错误(在迭代 200 处,字符数Arr5(200)已经接近 3k)。

感谢您的时间和帮助解决这个问题!

于 2013-11-05T23:51:54.980 回答