I’m encountering an error in Excel 2010 (32 bit version) when trying to initialize an array. The error specifically says: “Out of Memory” I should also note that I’m coding on a 64 bit machine with 32 GB of RAM.
This is the code I am using:
Sub ArrayTest()
Dim C As Variant
ReDim C(50257, 768) As Variant
Dim row As Integer
Dim column As Integer
For row = 1 To UBound(C, 1)
For column = 1 To UBound(C, 2)
C(row, column) = rnd
Next
Next
End Sub
The error occurs when I try and ReDim C().
What I tried so far:
I thought maybe I had hit the limitations of the Variant data type. So I tried to ReDim an array of type Double. I encountered the same error. I had success using a data type of Integer, but the issue is I need to capture floating point data with this array (as the Rnd functions shows).
Somebody had recommend I try using a Byte array. However Byte arrays can only capture integer data. So that didn’t work either. Another option I considered was using the Win32 API to see if it were possible to allocate the array in memory outside of Excel. Specifically the VirtualAlloc() function. I haven’t actually tried this yet as I’m not sure it will work. I’ve been told that if the underlying program you are using is 32 bits, then it won’t work.
Anyway, at this point I am perplexed as to what I should I do. I may consider using an Access database to store the data (50257 x 768 = 38,597,376 elements of data). This will slow down the code significantly though.
Are there any ways around this?
Long
instead ofInteger
. Also throwRandomize
in there too - well maybe at least give the docs a read and then decide whether you want it or not. – BigBen