1

现在我在A列中有这样的字符串:

aa-bb-cc-123

aa-bb-cc-345

aa-bb-cc-789

etc...

如何在 VBA 中用“-”分隔字符串并将每个子字符串粘贴到不同的列中?举个例子:

column W    column X    column Y    column Z

aa          bb          cc          123

aa          bb          cc          456

aa          bb          cc          789

任何帮助,将不胜感激!

4

3 回答 3

5

您可以尝试以下代码:

Sub split_AtoW()

Dim x As Long, mydata
For x = 1 To Range("A65526").End(xlUp).Row 'x = first row here, so change to 2 if header row exists
    mydata = split(Range("A" & x), "-") 'put data in column A into an array split by "-"
    Range("W" & x).Resize(1, UBound(mydata) + 1).Value = mydata 'put data in the split array into W onwards
Next x

End Sub
于 2013-10-07T16:12:02.480 回答
3

开始录制宏。准备好后,通过数据菜单,选择文本到列。跟着向导走到最后。停止录制。查看宏代码并根据需要进行修改。

于 2013-10-07T15:54:26.207 回答
1

选择您要处理的单元格并运行这个小宏:

Sub Splitter()
    Dim r As Range
    For Each r In Selection
        ary = Split(r.Value, "-")
        For i = LBound(ary) To UBound(ary)
            r.Offset(0, i + 1).Value = ary(i)
        Next i
    Next r
End Sub
于 2013-10-07T16:16:00.450 回答