VBA analysis of two-dimensional separator string in a range in excel

I have a two-dimensional string bounded by a string and inside each string, separated by a value.

So this is a comma-delimited line with an EOL token at the end of each line. Example:

val1, val2, val3 ... valn [EOL]
val1, val2, val3 ... valn [EOL]
...
val1, val2, val3 ... valn [EOL]

If I create a loop to split () each row using [EOL], then another loop inside this, to split () each value by ',' and then write each value one cell per sheet, which is required forever, therefore I am looking for a more effective solution.

Is it possible to parse a string into a 2D array / variant, and then write it all at once to a named range?

+4
source share
2

, . MultiSplit , . MultiSplit2 ( ). , .

Function MultiSplit(s As String, d1 As String, d2 As String) As Variant
    'd1 is column delimiter, d2 is row delimiter
    'returns an array

    Dim m As Long, n As Long, i As Long, j As Long
    Dim tempRows As Variant, tempRow As Variant
    Dim retA As Variant 'return array

    tempRows = Split(s, d2)
    m = UBound(tempRows)
    If Len(tempRows(m)) = 0 Then 'original string ends with a delimiter
        m = m - 1
        ReDim Preserve tempRows(m)
    End If

    tempRow = Split(tempRows(0), d1)
    n = UBound(tempRow)
    ReDim retA(1 To m + 1, 1 To n + 1) '1-based more natural for intended ranges

    For i = 1 To m + 1
        For j = 1 To n + 1
            retA(i, j) = tempRow(j - 1)
        Next j
        If i < m + 1 Then tempRow = Split(tempRows(i - 1), d1) ' next row to process
    Next i
    MultiSplit = retA
End Function

Sub test()
    Dim testString As String, A As Variant, R As Range
    testString = "a,b,c,d;e,f,g,h;i,j,k,l"

    A = MultiSplit(testString, ",", ";")
    Set R = Range(Cells(1, 1), Cells(UBound(A, 1), UBound(A, 2)))
    R.Value = A
End Sub

, :

Function MultiSplit2(s As String, d1 As String, d2 As String) As Variant
    'd1 is column delimiter, d2 is row delimiter
    'returns an array

    Dim m As Long, n As Long, i As Long, j As Long
    Dim tempRows As Variant, jaggedArray As Variant
    Dim retA As Variant 'return array

    tempRows = Split(s, d2)
    m = UBound(tempRows)
    If Len(tempRows(m)) = 0 Then 'original string ends with a delimiter
        m = m - 1
        ReDim Preserve tempRows(m)
    End If

    ReDim jaggedArray(0 To m)
    For i = 0 To m
        jaggedArray(i) = Split(tempRows(i), d1)
        If UBound(jaggedArray(i)) > n Then n = UBound(jaggedArray(i))
    Next i

    ReDim retA(1 To m + 1, 1 To n + 1) '1-based more natural for intended ranges

    For i = 1 To m + 1
        For j = 1 To 1 + UBound(jaggedArray(i - 1))
            retA(i, j) = jaggedArray(i - 1)(j - 1)
        Next j
    Next i
    MultiSplit2 = retA
End Function

Sub test2()
    Dim testString As String, A As Variant, R As Range
    testString = "a,b,c;d,e,f,g,h;i;j,k,l,m,n,o,p;"

    A = MultiSplit2(testString, ",", ";")
    Set R = Range(Cells(1, 1), Cells(UBound(A, 1), UBound(A, 2)))
    R.Value = A
End Sub

, sub , 1000 100 :

Sub test3()
    Dim s As String, A As Variant, R As Range
    Dim i As Long, j As Long, start As Double
    Dim n As Long

    For i = 1 To 1000
        n = i Mod 100
        For j = 1 To n
            s = s & "a" & IIf(j < n, ",", vbCrLf)
        Next j
        DoEvents 'in case it hangs
    Next i
    Debug.Print "String has length " & Len(s)
    start = Timer
    A = MultiSplit2(s, ",", vbCrLf)
    Set R = Range(Cells(1, 1), Cells(UBound(A, 1), UBound(A, 2)))
    R.Value = A
    Debug.Print "Finished in " & Timer - start & " seconds"
End Sub

, :

String has length 99990
Finished in 0.09375 seconds
+1

, @Macro Man . , -. , . .

Option Base 0

Sub test()

 sString = "val1, val2, val3 ... valn" & Chr(10) & "val1, val2 ... valn" & Chr(10) & "val1, val2, val3, val4 ... valn" & Chr(10) & "val1" & Chr(10)

 Dim aDataArray() As Variant
 Dim lLinesCount As Long
 Dim lValuesCount As Long
 Dim lMaxValuesCount As Long

 aLines = Split(sString, Chr(10))
 lLinesCount = UBound(aLines)
 ReDim aDataArray(0 To lLinesCount, 0)

 For i = LBound(aLines) To UBound(aLines)
  aValues = Split(aLines(i), ",")
  lValuesCount = UBound(aValues)
  If lValuesCount > lMaxValuesCount Then lMaxValuesCount = lValuesCount
  ReDim Preserve aDataArray(0 To lLinesCount, 0 To lMaxValuesCount)

  For j = LBound(aValues) To UBound(aValues)
   aDataArray(i, j) = aValues(j)
  Next
 Next

 With ActiveSheet
  .Range("B2").Resize(lLinesCount + 1, lMaxValuesCount + 1).Value = aDataArray
 End With

End Sub
+1

Source: https://habr.com/ru/post/1625617/


All Articles