View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Using Redim on 2 dimensional array

You can only change the last dimension. I has to do with the way that the
data is stored. A 2d array (or multi d for that matter) is stored on the disk
as a long string of memory. Here is an example:

A 2,4 array is actually stored as 4 blocks of 2. So when you redim the array
to 5 to then it just adds another block of 2 to the end (not strictly true as
it actually creates an an empty 2,5 array and then copies the old array over).
To redim the array to 3,4 would mean inserting an extra item into each block
of 4. That would require a whole pile of shuffling of memory to create all of
the required new memory slots. The overhead is just too great so it won't do
it.
--
HTH...

Jim Thomlinson


"RocketRod" wrote:

I am having trouble trying to use ReDim on a 2 dimensional array.
I need to increase the size of the array in a For€¦Next loop as data is
determined as valid - it is a staff list by name with 3 additional columns
of data for each name but the number of staff that will populate the array is
not known at the start, hence the Array needs to start as a 1x4, then change
to 2x4 etc

I have tried using both types of Dim statements as follows based on some of
the forum references
Dim StaffArray()
and
Dim StaffArray() €˜apparently this allows both dimensions of the
array to be modified in the ReDim

with each of the following types of ReDim (obviously only one at a time)

Dim staffindex as Integer
€¦
For€¦€¦.€¦
ReDim Preserve StaffArray(5, 4)
ReDim Preserve StaffArray(staffindex, 4)
ReDim Preserve StaffArray(staffindex To 5, 4)
ReDim Preserve StaffArray(staffindex To 5, 4 To 4)

Next€¦

Can some give me the right combination of Dim and ReDim please - I keep
getting subscript out of range errors