Excel 2007 VBA 数组大小限制 [英] Excel 2007 VBA Array Size Limit

查看:23
本文介绍了Excel 2007 VBA 数组大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的许多来源都表明 VBA 代码的数组大小取决于机器中的内存量.然而,这对我来说并非如此.我正在运行以下非常简单的代码进行测试:

Numerous sources I have found have suggested that the size of arrays for VBA code depends upon the amount of memory in the machine. This however hasn't been the case for me. I'm running the following, very simple, code to test:

Sub test6()
Dim arr(500, 500, 500) As Boolean
End Sub

但是,如果我将大小更改为 600x600x600,则会出现内存不足错误.我使用的机器有 16Gb 的 RAM,所以我怀疑物理 RAM 是问题所在.

However, if I change the size to be 600x600x600, I get an out of memory error. The machine I'm using has 16Gb of RAM, so I doubt that physical RAM is the issue.

我使用的是 Excel 2007.有没有让 VBA 使用更多 RAM 的技巧?

I'm using Excel 2007. Is there a trick to getting VBA to use more RAM?

推荐答案

如果有一个我们可以调用的 Application.UseMoreMemory() 函数就好了 :-)

It would be nice if there was an Application.UseMoreMemory() function that we could just call :-)

唉,我不知道.

我看过的所有文档都说它受内存限制,但这不是物理内存的问题,而是您可以使用的虚拟地址空间.

All the docs I've seen say that it's limited by memory, but it's not physical memory that's the issue, it's the virtual address space you have available to you.

你应该记住,虽然从 500 增加到 600 看起来只是适度的增加(尽管 20% 本身就足够大),因为你是在三个维度上这样做的,结果是接近两倍的存储要求.

You should keep in mind that, while the increase from 500 to 600 only looks like a moderate increase (though 20% is large enough on its own), because you're doing that in three dimensions, it works out to be close to double the storage requirements.

在内存中,Excel 2007 使用短整数(16 位)作为布尔类型,因此,您的 5003 数组至少将占用大约 250M(500x500x500x2).

From memory, Excel 2007 used short integers (16 bits) for boolean type so, at a minimum, your 5003 array will take up about 250M (500x500x500x2).

将所有尺寸增加到 600 会得到 600x600x600x2,或大约 432M.

Increasing all dimensions to 600 would give you 600x600x600x2, or about 432M.

在 32 位机器上可能拥有的 2G 可用地址空间内一切都很好(我不知道 Excel 2007 64 位版本),但是这些东西小,并且您还必须与其他事物共享该地址空间.

All well within the 2G usable address space that you probably have in a 32-bit machine (I don't know that Excel 2007 had a 64-bit version), but these things are not small, and you have to share that address space with other things as well.

看看您在什么时候开始收到错误会很有趣.

It'd be interesting to see at what point you started getting the errors.

作为第一步,我会研究是否需要如此大的数组.这可能是一种不同的方式,例如对数组进行分区,以便在任何时候只有一部分在内存中(类似于手动虚拟内存).

As a first step, I'd be looking into the need for such a large array. It may be doable a different way, such as partitioning the array so that only part of it is in memory at any one time (sort of manual virtual memory).

对于真正的随机访问来说,这不太可能表现得那么好,但对于更多的顺序访问来说应该不会太糟糕,并且至少会让你前进(慢的解决方案比不工作的解决方案更可取).

That's unlikely to perform that well for truly random access but shouldn't be too bad for more sequential access and will at least get you going (a slow solution is preferable to a non-working one).

另一种可能性是抽象掉位处理,以便您的布尔值实际上存储为位而不是字.

Another possibility is to abstract away the bit handling so that your booleans are actually stored as bits rather than words.

您必须为 getBoolsetBool 提供函数,在单词数组上使用位掩码运算符,同样,性能不会那么火爆,但您至少可以达到以下效果:

You would have to provide functions for getBool and setBool, using bitmask operators on an array of words and, again, the performance wouldn't be that crash-hot, but you would at least be able to then go up to the equivalent of:

' Using bits instead of words gives 16 times as much. '
Dim arr(8000, 8000, 8000) As Boolean

一如既往,这取决于您需要数组的用途及其使用模式.

As always, it depends on what you need the array for, and its usage patterns.

这篇关于Excel 2007 VBA 数组大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆