Excel macro -Split comma separated entries to new rows

后端 未结 5 698
说谎
说谎 2020-12-07 00:20

I currently have this data in a sheet

Col A   Col B   Col C
1       A       angry birds, gaming
2       B       nirvana,rock,band

What I wa

5条回答
  •  太阳男子
    2020-12-07 00:38

    variant using Scripting.Dictionary

    Sub ttt()
        Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
        Dim x&, cl As Range, rng As Range, k, s
        Set rng = Range([C1], Cells(Rows.Count, "C").End(xlUp))
        x = 1 'used as a key for dictionary and as row number for output
        For Each cl In rng
            For Each s In Split(cl.Value2, ",")
                dic.Add x, Cells(cl.Row, "A").Value2 & "|" & _
                            Cells(cl.Row, "B").Value2 & "|" & LTrim(s)
                x = x + 1
        Next s, cl
        For Each k In dic
            Range(Cells(k, "A"), Cells(k, "C")).Value2 = Split(dic(k), "|")
        Next k
    End Sub
    

    source:

    result:

提交回复
热议问题