5

分割コマンドを使用して配列を設定するのに問題があります。

私が現在持っている文字列は以下のとおりです

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"

多次元にしたい配列があり、各 Row# Column# をその番号に基づいて配列の正しい部分に配置したいと考えています。

例えば

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"

これで、分割コマンドを使用して 1 次元配列を作成する方法を理解できました

MyArray = Split(MyString, vbNewLine)

これは次のことを意味します。

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"

しかし、分割コマンドを使用して 2 番目の次元を設定する方法がわかりません。

これは可能ですか?
それが不可能な場合、実際にこれを設定する方法を誰かが提案できますか?

4

3 に答える 3

5

Split() は、文字列または文字列を含むバリアント以外には使用できません。2 次元の文字列配列を生成する場合は、Split() によって返された配列を反復処理し、各文字列に対して Split() を実行します。次の関数は、あなたが望むことをするはずです:

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

例:

Dim asCells() As String

asCells() = SplitTo2DArray(MyString, vbNewline, "~")
于 2012-05-16T09:30:33.980 に答える
0

Here is another approach based on the Variant type's ability to contain an array. Instead of a 2D array we have an "array of arrays."

Option Explicit

Private Function SplitSplit(ByRef Delimited As String) As Variant
    Dim Rows() As String
    Dim AryOfArys As Variant
    Dim I As Long

    Rows = Split(Delimited, vbNewLine)
    ReDim AryOfArys(UBound(Rows))
    For I = 0 To UBound(Rows)
        AryOfArys(I) = Split(Rows(I), "[~]")
    Next
    SplitSplit = AryOfArys
End Function

Private Sub Form_Load()
    Dim MyString As String
    Dim MyAry As Variant

    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"

    MyAry = SplitSplit(MyString)

    AutoRedraw = True
    DumpAry MyAry
End Sub

Private Sub DumpAry(ByRef AryOfArys As Variant)
    Dim Row As Long, Col As Long

    For Row = 0 To UBound(AryOfArys)
        For Col = 0 To UBound(AryOfArys(Row))
            Print AryOfArys(Row)(Col),
        Next
        Print
    Next
End Sub

The bonus here is "ragged arrays" where each row can have a different number of columns.

于 2012-05-19T18:45:42.257 に答える