Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ReDim Problems

Status
Not open for further replies.

slawson7

Technical User
Apr 23, 2007
7
GB
Following on from my previous question on arrays (thanks for the help on that, BTW). I have the following code:

Dim myArry (1,3)
myArray (1,1) = "x"
myArray(1,2) = 1
myArray (1,3) = 1
.
.
.
ReDim Preserve myArray (2,3)

The ReDim statement throws a compilation error, "Array already dimensioned." Why?
 
If you use the Preserve keyword, you can resize only the last array dimension, and you're Dim's should be like this:
Code:
Dim myArray()
ReDim myArray(1, 3)
myArray(1, 1) = "x"
myArray(1, 2) = 1
myArray(1, 3) = 1

ReDim Preserve myArray(1, 5)


Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 



Hi,

Only the LAST dimension can be changes
Code:
    Dim myArry()
    
    ReDim myArry(3, 1)
    
    myArry(1, 1) = "x"
    myArry(2, 1) = 1
    myArry(3, 1) = 1
    
    ReDim Preserve myArry(3, 2)

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Also, in case it isn't obvious from the previous 2 posts, watch how you declare the array.
Use
Dim myArry()
and not
Dim myArry(1,3)
 
Thanks Glenn & Skip (again!).

Being an ex-DBA, I think of two-dimensional arrays as tables, with rows and columns. I start out with a 'table' that has three 'rows' and one 'column'.

So to add another column, I would have to do:
redim myArray (2,1)
redim myArray (2,2)
redim myArray (2,3)

...right?
 




Think of it in any way you like. Does that chnage anything?

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top