LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Dim / Redim of an Array


I have an Array that needs to be filled with several values.
I do not know beforehand how many values will be necessary, so I Dim it to
50 values to be on the safe side and Redim it to the actual amount after
calculation (10 in this example)
Logical thinking says I should do that the following way:

1. Dim Arr(50)
2. Fill array with values (after which necessary dimension is known)
3. Redim Preserve Arr(10)

This code gives me the error that the dimension of the matrix is already
set. So I replaced the first line with Dim Arr(), followed by ReDim(50) as
shown in the follwing code (which works):

1. Dim Arr()
2. Redim Arr(50)
3. Fill array with values (after which necessary dimension of 10 is known)
4. Redim Preserve Arr(10)

My first question is: What's wrong with the first section of code ?


OK, So this seems to work now, BUT I actually need a 2 column
multidimensional Array, so I thought this should work:

1. Dim Arr()
2. Redim Arr(50,1)
3. Fill array with values (after which necessary dimension of 10 is known)
4. Redim Preserve Arr(10,1)

But, you guessed, I get an error (nr. 9, out of range) on the "ReDim
Preserve Arr(10, 1)" statement in the example code below. Can anyone tell me
why this doesn't work ?

Sub MDArrayTest()

Dim i As Long
Dim Arr()

ReDim Arr(50, 1)

For i = 0 To 10
Arr(i, 0) = "Col 0: " & i
Arr(i, 1) = "Col 1: " & i
Next

ReDim Preserve Arr(10, 1)

For i = LBound(Arr) To UBound(Arr)
Debug.Print i & " - " & Arr(i, 0) & " - " & Arr(i, 1)
Next

End Sub


Thanx !


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA syntax help: ReDim Preserve an array Dave Excel Discussion (Misc queries) 4 September 8th 07 07:37 PM
Redim 2D Array Subscript Out Of Range Error lopsided[_10_] Excel Programming 6 February 11th 04 08:24 AM
ReDim Problem Casey[_4_] Excel Programming 4 January 7th 04 10:41 PM
ReDim an Array Art[_5_] Excel Programming 3 October 25th 03 03:30 PM
Redim MyArray Peter Pantus Excel Programming 2 September 27th 03 03:37 PM


All times are GMT +1. The time now is 01:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"