EXCEL如何将文本拆分为不同的单元格并垂直排列?

时间:2014-03-02 14:45:27

标签: excel text split cell

我想处理大量数据。手动操作是不可能的。 我想把源(例如1)变成像(例如2)的形式。

[例如1]
A1; A2; A3
B1; B2; B3; B4
C1; C2

[例如2]
A1
A2
A3
B1
B2
B3
B4
C1
C2

但是,我只能将其转换为这种形式。
a1 a2 a3
b1 b2 b3 b4
c1 c2

=============================================== ================================ 非常感谢你。我还有另外一件事要做。 我想更进步。

让x成为任何# 这里是21

[例如1]
A1; A2; A3
B1; B2; B3; B4
C1,C2

[例如2]
21 a1
21 a2
21 a3
22 b1
22 b2
22 b3
22 b4
23 c1
23 c2

空间意味着它们位于不同的单元格中。

感谢~~

1 个答案:

答案 0 :(得分:0)

假设 a1; a2; a3 位于单元格 A1 中,请尝试使用此小宏将结果放在 B 列中:

Sub ReArrange()
    Dim N As Long, i As Long, BigString As String
    N = Cells(Rows.Count, 1).End(xlUp).Row
    BigString = Range("A1").Value
    For i = 2 To N
        BigString = BigString & ";" & Cells(i, 1).Value
    Next i
    i = 1
    ary = Split(BigString, ";")
    For Each a In ary
        Cells(i, 2).Value = a
        i = i + 1
    Next a
End Sub

修改#1

根据您对额外列的需求,此宏会询问初始值并将结果放在 A B 列中

Sub ReArrangeII()
    Dim N As Long, i As Long, BigString As String
    Dim M As Long, j As Long
    N = Cells(Rows.Count, 1).End(xlUp).Row
    M = Application.InputBox(Prompt:="enter initial value", Type:=1)
    j = 1
    For i = 1 To N
        BigString = Cells(i, 1).Value
        ary = Split(BigString, ";")
        u = UBound(ary) + 1
        For Each a In ary
            Cells(j, 2).Value = M
            Cells(j, 3).Value = a
            j = j + 1
        Next a
        M = M + 1
    Next i
End Sub