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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA three dimensional array of unknown size

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
0
0
GB

I currently have three single arrays working but I would like to replace them with one 3 dimensional array. I have been trying without success for hours.

Here is the working code for the three arrays:

I need the code to achieve the same thing but using one 3 dimensional array instead of the three arrays I currently have.

I declare 3 global arrays
Private aOne() As Single
Private aTwo() As Single
Private aThree() As Double

I run this once for first size
ReDim aOne(1 To 1)
ReDim aTwo (1 To 1)
ReDim aThree(1 To 1)

I populate the 3 arrays and resize them
aOne(UBound(aOne)) = MyVariableOne
aTwo(UBound(aTwo)) = MyVariableTwo
aThree(UBound(aThree)) = MyVariableThree
ReDim Preserve aOne(1 To UBound(aOne) + 1)
ReDim Preserve aTwo (1 To UBound(aTwo) + 1)
ReDim Preserve aThree(1 To UBound(aThree) + 1)

I retrieve the values
Dim i As Long
For i = LBound(aOne) To UBound(aOne) - 1
msgbox aOne
msgbox aTwo
msgbox aThree
Next
 
First, arrays are 0 based, so why do you make them start with 1?
2. Array can be only one type, you have 2 arrays As Single and 1 array As Long. You need to decide what type of 3-D array you need.
3. To declare, use, redim, preserve, etc. multi-dimensional array, look here

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
hank you Andrzejek I will take a look and try to work it out.
 
From your example it looks like you need 2D array. If all data can be double, you can:
[tt]Redim Preserve anAllInOne(1 To 3, 1 To Ubound(anAllInOne, 2)+1)[/tt]
You can resize only last dimension.

combo
 
>First, arrays are 0 based

Not always.

>2. Array can be only one type, you have 2 arrays As Single and 1 array As Long

Or we can declare as variant ...

> it looks like you need 2D array

yep, that looks to be the case
 
If the content of arrays with the same index is logically linked, it may be useful to build user defined type with required structure and have array with one dimension only.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top