一种方法是先在内存中组装一个数组,然后在一行代码中传输它。第一个函数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
为了获取一些时间信息,我编写了一个子程序来生成一个字符串,该字符串分为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
UBound()
和Resize()
直接将数组输入到范围中,而无需循环。 - SierraOscar