Array and Split commands to create a 2 dimensional array

arrays, multidimensional-array, split, vb6

Solution

You can't use Split() on anything other than a String, or a Variant containing a String. If you want to generate a two dimensional string array, you will have iterate through the array returned by Split(), and run Split() on each string. The following function should do what you want:

Private Function SplitTo2DArray(ByRef the_sValue As String, ByRef the_sRowSep As String, ByRef the_sColSep As String) As String()

    Dim vasValue                    As Variant
    Dim nUBoundValue                As Long
    Dim avasCells()                 As Variant
    Dim nRowIndex                   As Long
    Dim nMaxUBoundCells             As Long
    Dim nUBoundCells                As Long
    Dim asCells()                   As String
    Dim nColumnIndex                As Long

    ' Split up the table value by rows, get the number of rows, and dim a new array of Variants.
    vasValue = Split(the_sValue, the_sRowSep)
    nUBoundValue = UBound(vasValue)
    ReDim avasCells(0 To nUBoundValue)

    ' Iterate through each row, and split it into columns. Find the maximum number of columns.
    nMaxUBoundCells = 0
    For nRowIndex = 0 To nUBoundValue
        avasCells(nRowIndex) = Split(vasValue(nRowIndex), the_sColSep)
        nUBoundCells = UBound(avasCells(nRowIndex))
        If nUBoundCells > nMaxUBoundCells Then
            nMaxUBoundCells = nUBoundCells
        End If
    Next nRowIndex

    ' Create a 2D string array to contain the data in <avasCells>.
    ReDim asCells(0 To nUBoundValue, 0 To nMaxUBoundCells)

    ' Copy all the data from avasCells() to asCells().
    For nRowIndex = 0 To nUBoundValue
        For nColumnIndex = 0 To UBound(avasCells(nRowIndex))
            asCells(nRowIndex, nColumnIndex) = avasCells(nRowIndex)(nColumnIndex)
        Next nColumnIndex
    Next nRowIndex

    SplitTo2DArray = asCells()

End Function

Example:

Dim asCells() As String

asCells() = SplitTo2DArray(MyString, vbNewline, "~")

Problem

I'm having some trouble populating an array using a split command. The string I currently have is below ``` MyString = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" & vbNewLine & _ "Row2 Column1[~]Row2 Column2[~]Row2 Column3" & vbNewLine & _ "Row3 Column1[~]Row3 Column2[~]Row3 Column3" & vbNewLine & _ "Row4 Column1[~]Row4 Column2[~]Row4 Column3" ``` I have an array that I want to be multi-dimensional and would like each Row# Column# to be in the correct part of the array based on its number. For example ``` MyArray(1,1) = "Row1 Column1" MyArray(2,1) = "Row2 Column1" MyArray(3,1) = "Row3 Column1" MyArray(4,1) = "Row4 Column1" MyArray(1,2) = "Row1 Column2" MyArray(2,2) = "Row2 Column2" MyArray(3,2) = "Row3 Column2" MyArray(4,2) = "Row4 Column2" MyArray(1,3) = "Row1 Column3" MyArray(2,3) = "Row2 Column3" MyArray(3,3) = "Row3 Column3" MyArray(4,3) = "Row4 Column3" ``` Now I understand how to populate a single dimension array using the split command ``` MyArray = Split(MyString, vbNewLine) ``` This would mean that ``` MyArray(1) = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" MyArray(2) = "Row2 Column1[~]Row2 Column2[~]Row2 Column3" MyArray(3) = "Row3 Column1[~]Row3 Column2[~]Row3 Column3" MyArray(4) = "Row4 Column1[~]Row4 Column2[~]Row4 Column3" ``` But I don't know how to use a split command to populate the second dimension. Is this possible and if it is how? If it isn't possible, can anyone suggest how to actually populate this?

Original source