简体   繁体   English

VBA将数组写入范围,导致运行时错误“ 7”:内存不足

[英]VBA Writing Array to Range Causing Run-Time Error '7': Out Of Memory

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. 我工作的VBA项目,其中我从存储在一个表中的数据Variant Array ,做了一堆用在计算数据的Array ,这些计算的结果存储在其他几个Arrays ,然后最后我存储所有的“结果阵列”在另一Variant Array和写在一个新的工作表数组的范围。

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. 进行很小的逻辑更改后,我的最终Array将不再写入指定的范围。 I get Run-time error '7': Out of memory when I try. 我收到Run-time error '7': Out of memory尝试时Run-time error '7': Out of memory 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"). 我已经检查了任务管理器,并显示我的RAM具有很多可用空间(将近6K MB可用,超过1k Mb“可用”)。 (Task Manager shows the Excel process as using around 120 MBs.) (任务管理器显示Excel进程使用了​​大约120 MB。)

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 编辑:数组大小:773x5

Any thoughts on why this might be happening? 对为什么会发生这种情况有任何想法吗?

I figured out the issue. 我发现了问题。

Arr5 (a string array) was the culprit. 罪魁祸首是Arr5 (字符串数组)。 Each element in that array is set like so: 该数组中的每个元素设置如下:

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

I forgot to include this line to reset StrVariable after each loop: 我忘了在每次循环后都包含以下行来重置StrVariable

 StrVariable = vbNullString

The result meant that by the 773 iteration, Arr5(773) had a massive string length...so massive, in fact, that even though my code could process the array, the sheet could not. 结果意味着,通过773迭代,Arr5(773)的字符串长度很大……实际上如此之大,即使我的代码可以处理数组,工作表也不能。

I discovered the error by stopping at iteration 200 and checking the output of All into the sheet (at iteration 200 the character count of Arr5(200) was already almost 3k). 我通过在迭代200处停止并检查All到表中的输出发现了错误(在迭代200中, Arr5(200)的字符数已经接近3k)。

Thank you for your time and help with this problem! 感谢您的时间,并为您解决此问题提供帮助!

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

相关问题 运行时错误9:访问简单数组时索引超出范围 - Run-time error 9: Index out of range when accessing a simple array 使用VBA将多个Excel工作表打印到一个pdf并获得运行时错误&#39;9&#39;:选择选项卡时下标超出范围 - Printing multiple excel worksheets to one pdf using VBA and getting Run-time error '9': subscript out of range when selecting tabs 在Excel 2010 VBA中使用变体数组的运行时错误 - Run-time error using variant array in Excel 2010 VBA 运行时错误9:使用字符串数组时下标超出范围 - Run-time error 9: Subscript out of range while working with string arrays VBA运行时错误13-类型不匹配-价格时间序列计算不适用于数组 - VBA Run-time error 13 - type mismatch- price time series calc not working on array 奇怪的数组运行时错误 - Strange array run-time error Excel VBA,取消隐藏数组中的工作表,运行时错误“13”:类型不匹配 - Excel VBA, UnHiding Sheets in array, Run-time error '13': Type mismatch 运行时错误9 - Run-time error 9 相对于构造函数抛出的超出范围的运行时错误 - Out-of-bounds run-time error thrown relative to constructor 在 VBA 中显示来自维度 JSON 数组的数据,收到“运行时错误 &#39;13&#39;:类型不匹配”错误消息 - Display data from dimensional JSON array in VBA, getting “Run-time error '13': Type mismatch” an error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM