在excel vba中拆分多个字符串

时间:2013-11-17 20:24:55

标签: excel vba excel-vba

我有一个excel,其中一些数据用“;”分隔在一个牢房里。 我需要拆分该单元格,以及后续的单元格,以便拆分的内容每个都分别为新行。 例如:

Column f    Column j     Column k
a;b;c       d;e;f        g;h;i

应该成为

a         d       g
b         e       h
c         f       i

这就是我所拥有的,但不起作用:

Sub tgr()

Dim rindex As Long
Dim saItem() As String
Dim sbItem() As String
Dim scItem() As String

For rindex = Cells(Rows.Count, "F").End(xlUp).Row To 1 Step -1
    If InStr(Cells(rindex, "F").Value, ";") > 0 Then
        saItem = Split(Cells(rindex, "F").Value, ";")
        sbItem = Split(Cells(rindex, "J").Value, ";")
        scItem = Split(Cells(rindex, "K").Value, ";")
        Rows(rindex + 1 & ":" & rindex + UBound(saItem)).Insert
        Cells(rindex, "F").Resize(UBound(saItem) + 1).Value = WorksheetFunction.Transpose(saItem)
        Cells(rindex, "J").Resize(UBound(sbItem) + 1).Value = WorksheetFunction.Transpose(sbItem)
        Cells(rindex, "K").Resize(UBound(scItem) + 1).Value = WorksheetFunction.Transpose(scItem)
    End If
Next rindex

End Sub

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您使用错误的阵列进行Col K.我猜您复制粘贴并忘记更改它? :)

替换

Cells(rindex, "K").Resize(UBound(sbItem) + 1).Value = WorksheetFunction.Transpose(sbItem)

Cells(rindex, "K").Resize(UBound(scItem) + 1).Value = WorksheetFunction.Transpose(scItem)