Multidimensional Array Help

jaybee3

Active Member
Joined
Jun 28, 2010
Messages
307
Arrays always do my head in. Find a simple piece of code of what I'm trying to do.

As I would normally throw variables into an array using:

aTest = array(1,2,3,4,5)

Is it possible to do this for multiple dimensional ones?

Code:
Dim sTest(4, 4) As String
Dim ii As Integer, jj As Integer
sTest(0) = Array("hi", "Hello", "Bye", "No", "yes")
sTest(1) = Array("salut", "bonjour", "au revoir", "non", "Oui")
For ii = 0 To sTest.Rank - 1
    For jj = LBound(sTest(ii)) To UBound(sTest(ii))
        Debug.Print sTest(ii, jj)
    Next jj
Next ii
End Sub
 
Now tested, works OK here.
How have you dimmed the array(s) and what variable type are you trying to assign it/them?
 
Upvote 0
As above, array only dimmed (as variant). Tried to assign both a string and an integer to the array.

The workaround I found was including sTest(1)=Array("","","","","") and then replacing them with sTest(1)(ii) = ii in the loop. If I didn't initialise them it would throw an error.

Full code:
Rich (BB code):
Dim sTest(4)
Dim ii As Integer, jj As Integer
sTest(0) = Array("hi", "Hello", "Bye", "No", "yes")
sTest(1) = Array("", "", "", "", "")
For ii = 0 To 4
  sTest(1)(ii) = ii
Next ii
For ii = 0 To 1
  For jj = LBound(sTest(ii)) To UBound(sTest(ii))
    Debug.Print sTest(ii)(jj)
  Next jj
Next ii
 
Upvote 0
so i think you are/were still thinking about jagged arrays somewhat the same as multi-dimensional arrays.

jagged arrays are really just arrays stored at some index of another array, and as such you have to set them explicitly, you can't dim a jagged array.

i never really liked jagged arrays, but they are useful sometimes, just a little tricky to iterate through.

this is a thrilling recursive function that "straightens" out jagged arrays:

Code:
Option Explicit
Option Base 0

Private tempArray As Variant
Private incAdd As Long, uBnd As Long
Private cnt As Long

Public Function getOneDimArray(inputVar, _
                                Optional expectedSize As Long = 5000, _
                                Optional incrementalAdd As Long = 1000, _
                                Optional tst As Boolean _
                                ) As Variant()
Dim tmpArr As Variant

tst = False
On Error GoTo exitFunc
'set global variables to input
If expectedSize < 1 Then Exit Function
uBnd = expectedSize
If incrementalAdd < 1 Then Exit Function
incAdd = incrementalAdd

'redim temparray to the expected size (input)
ReDim tempArray(0 To uBnd)
cnt = 0: uBnd = 0

'actually call function
Call recurseOneDim(inputVar)

If cnt > 0 Then
    ReDim Preserve tempArray(0 To cnt - 1)
    getOneDimArray = tempArray
    tst = True
End If

exitFunc:
End Function


'simple recursive function to "straighten out" any variant array
'very slow should be used only in specific circumstances
'will not currently work with objects etc...easily adapted
Private Function recurseOneDim(testArray)

Dim tVal As Variant

On Error GoTo exitFunc

For Each tVal In testArray
    If Not isArray(tVal) Then
        If cnt > uBnd Then
            uBnd = uBnd + incAdd
            ReDim Preserve tempArray(0 To uBnd)
        End If
        
        tempArray(cnt) = tVal
        cnt = cnt + 1
    Else
        Call recurseOneDim(tVal)
    End If
Next

exitFunc:
End Function
 
Upvote 0
Instead of:
sTest(1) = Array("", "", "", "", "")
you could:
Dim xxx(0 To 4)
sTest(1) = xxx
 
Upvote 0

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