Excel 2010 VBA - 以逗号分割字符串,跳过空白结果

时间:2016-02-16 02:42:55

标签: arrays excel vba excel-vba delimited

我使用以下代码来删除一列以逗号分隔的列表并返回新行中的每个条目:

Sub SliceNDice()
    '
    ' Splits the locations cells according to commas and pushes to new rows
    ' Code courtesy of brettdj (http://stackoverflow.com/questions/8560718/split-comma-separated-entries-to-new-rows)
    '
    Dim objRegex As Object
    Dim x
    Dim Y
    Dim lngRow As Long
    Dim lngCnt As Long
    Dim tempArr() As String
    Dim strArr
    Set objRegex = CreateObject("vbscript.regexp")

    objRegex.Pattern = "^\s+(.+?)$"
     'Define the range to be analysed
    x = Range([a1], Cells(Rows.Count, "c").End(xlUp)).Value2
    ReDim Y(1 To 3, 1 To 1000)
    For lngRow = 1 To UBound(x, 1)
         'Split each string by ","
        tempArr = Split(x(lngRow, 3), ",")
        For Each strArr In tempArr
            lngCnt = lngCnt + 1
             'Add another 1000 records to resorted array every 1000 records
            If lngCnt Mod 1000 = 0 Then ReDim Preserve Y(1 To 3, 1 To lngCnt + 1000)
            Y(1, lngCnt) = x(lngRow, 1)
            Y(2, lngCnt) = x(lngRow, 2)
            Y(3, lngCnt) = objRegex.Replace(strArr, "$1")
        Next
    Next lngRow
     'Dump the re-ordered range to columns E:G
    [e1].Resize(lngCnt, 3).Value2 = Application.Transpose(Y)

End Sub

虽然这段代码完美无缺,但它有一个致命的缺陷,即C列单元格中的任何双逗号都会导致空白单元格被推到G列的新行。

有没有人知道如何编辑代码,以便它不会在G列中创建包含空单元格的新行,而是跳过它们并在其位置输入下一行,就好像多余的逗号从未包含在C列中一样?

3 个答案:

答案 0 :(得分:2)

只测试strArr的字符串长度作为For EachArrr在tempArr循环中的第一个操作。

For Each strArr In tempArr
    If CBool(Len(strArr)) Then
        lngCnt = lngCnt + 1
         'Add another 1000 records to resorted array every 1000 records
        If lngCnt Mod 1000 = 0 Then ReDim Preserve Y(1 To 3, 1 To lngCnt + 1000)
        Y(1, lngCnt) = x(lngRow, 1)
        Y(2, lngCnt) = x(lngRow, 2)
        Y(3, lngCnt) = objRegex.Replace(strArr, "$1")
    End If
Next strArr

答案 1 :(得分:1)

你可以循环使用双逗号来清理输入而不是修复输出,这是一个有效的例子:

A1中的文字:Hello,,World,This,,Is,,,,,,,A,,Test

Sub TestString()
Dim MyString As String
MyString = Range("A1").Text
Do Until Len(MyString) = Len(Replace(MyString, ",,", ","))
    MyString = Replace(MyString, ",,", ",")
Loop
MsgBox MyString
End Sub

您可以在拆分之前执行此操作

如果你想将它作为一个函数(在你的情况下会更好),请执行以下操作:

Function FixDoubleComma(MyString As String)
Do Until Len(MyString) = Len(Replace(MyString, ",,", ","))
    MyString = Replace(MyString, ",,", ",")
Loop
FixDoubleComma = MyString
End Function

然后在代码中替换它:

tempArr = Split(x(lngRow, 3), ",")

有了这个:

tempArr = Split(FixDoubleComma(x(lngRow, 3)), ",")

答案 2 :(得分:0)

我有一个小样,可以解决所有地方的空白

Sub RemoveBlanks()
    Dim mystr As String
    Dim arrWithBlanks() As String
    Dim arrNoBlanks() As String
    Dim i As Integer
    mystr = ",tom,jerry, ,,spike,," 'Blanks everywhere (beginning, middle and end)
    arrWithBlanks = Split(mystr, ",")
    ReDim arrNoBlanks(0 To 0)
    Debug.Print "Array with blanks:"
    'Loop through the array with blanks
    For i = LBound(arrWithBlanks) To UBound(arrWithBlanks)
        'Check if there is a blank (or element with spaces only)
        If Trim(arrWithBlanks(i)) = "" Then
            Debug.Print i & " (blank)"
        Else
            Debug.Print i & " " & arrWithBlanks(i)
            If arrNoBlanks(UBound(arrNoBlanks)) <> "" Then ReDim Preserve arrNoBlanks(0 To UBound(arrNoBlanks) + 1)
            arrNoBlanks(UBound(arrNoBlanks)) = arrWithBlanks(i)
        End If
    Next i
    Debug.Print "Array with NO blanks:"
    For i = LBound(arrNoBlanks) To UBound(arrNoBlanks)
        Debug.Print i & " " & arrNoBlanks(i)
    Next i
End Sub

所有内容都会显示在即时窗口中(按 Ctrl + G 进行显示)

结果将如下所示:

Array with blanks:  
0 (blank)
1 tom
2 jerry
3 (blank)
4 (blank)
5 spike
6 (blank)
7 (blank)
Array with NO blanks:
0 tom
1 jerry
2 spike