通过分隔符拆分字符串并垂直转置值

时间:2015-04-16 19:06:48

标签: excel excel-formula

我在excel中有一个用逗号分隔的列(,)

       column  
cell1:  1,2,3
cell2:   2,4

我想拆分每个单元格,使值显示在垂直单元格中,以便每个单元格转换为

cell1 => split into 3 vertical cells containing values 1,2,3 
cell2 => split into 2 vertical cells containing values 2,4 

我发现了很多方法可以横向完成这项工作,但是在垂直细胞上挣扎。

2 个答案:

答案 0 :(得分:1)

从以下数据开始:

enter image description here

首先选择您要处理的数据并运行此宏:

Sub Vertical()
    Dim i As Long, st As String
    i = 1
    Dim startP As Range
    For Each r In Selection
        If i = 1 Then
            st = r.Text
            i = 2
        Else
            st = st & "," & r.Text
        End If
    Next r

    Set startP = Selection(1, 2)
    ary = Split(st, ",")
    i = 1
    For Each a In ary
        startP(i, 1).Value = a
        i = i + 1
    Next a
End Sub

生产:

enter image description here

修改#1:

要将每个单元格从水平显示的逗号分隔列表转换为垂直间隔的逗号分隔列表,请运行此宏:

Sub Vertical2()
    Dim r As Range
    For Each r In Selection
        r.Value = Replace(r.Value, ",", "," & vbCrLf)
        r.WrapText = True
    Next r
End Sub

在选定的单元格上:

enter image description here

答案 1 :(得分:-1)