Creating multiple arrays with minimal code

kingofaces

Board Regular
Joined
Aug 23, 2010
Messages
68
Basically, I'm trying to figure out the best way to create multiple arrays in VBA. Each array will be for a different location (n in the code below). Later on in a much larger macro I would like to be able to do calculations involving each location, so I want to reference to that specific array.

Code:
Sub test()

a = WorksheetFunction.RandBetween(1, 20)

For n = 1 To 10
For x = 1 To 5
For y = 1 To 2

DDarray(x, y) = a

Next x
Next y
Next n

End Sub

This is similar to what I'm trying to do. For each n (up to 10 here). I would like to create a new DDarray variable. Instead of repeating this code for DDarray1, DDarray2, . . . DDarray10, is there a way to keep the code concise like above? The n variable can change, so I will not know the number of arrays needed, and can't simply do anything like this:

Code:
Sub test()

a = WorksheetFunction.RandBetween(1, 20)

For x = 1 To 5
For y = 1 To 2

DDarray1(x, y) = a

Next x
Next y

For x = 1 To 5
For y = 1 To 2

DDarray2(x, y) = a

Next x
Next y

'and so on
End Sub

Any ideas? I don't think there's a way to make part of a variable name another variable in itself is there?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I should reiterate a little bit, but the main question here is if there is a way to use one variable in a loop to create multiple variable names? If there is it doesn't seem intuitive, but that would solve all my problems right there.
 
Upvote 0
You could make a 3 dimensional array DDArray(x,y,n)

Code:
Sub test()
    
    Dim DDArray() As Variant

   
    ReDim DDArray(1 To 5, 1 To 2, 1 To 1) 'DDArray(x,y,n)

    For n = 1 To 3
        If n > 1 Then ReDim Preserve DDArray(1 To 5, 1 To 2, 1 To n) 'You can only ReDim the last dimention in the array
        For x = 1 To 5
            For y = 1 To 2
                DDArray(x, y, n) = Int((20 * Rnd) + 1) ' Generate random value between 1 and 20
            Next y
        Next x
    Next n
    
    ' Example output
    For n = 1 To UBound(DDArray, 3)
        For x = 1 To 5
            strTemp$ = strTemp$ & "DDArray" & n & "   " & x & ":1=" & DDArray(x, 1, n) & "   " & x & ":2=" & DDArray(x, 2, n) & vbLf
        Next x
    Next n
    
    MsgBox strTemp$

End Sub
 
Last edited:
Upvote 0
Talk about the answer being right in front of my nose. I was too focused on separate arrays to remember you can have more than two dimensions in an array. Thanks a bunch.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top