Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel 2007 VBA Array Size Limit

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

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.

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

like image 670
Farthingworth Avatar asked Oct 19 '11 06:10

Farthingworth


People also ask

How big can an array be in Excel VBA?

The limit is less than the number of rows by the number of columns in the worksheet which is 1048576 rows by 16384 columns (Excel specifications and limits).

How do I change the size of an array in VBA?

The ReDim statement is used to size or resize a dynamic array that has already been formally declared by using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). Use the ReDim statement repeatedly to change the number of elements and dimensions in an array.

How many types of arrays in Excel VBA explain?

There are two types of VBA arrays: Static – an array of fixed length. Dynamic(not to be confused with the Excel Dynamic Array) – an array where the length is set at run time.


1 Answers

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

Alas, I know of none.

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.

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.

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

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

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.

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.

like image 91
paxdiablo Avatar answered Sep 20 '22 07:09

paxdiablo