我目前在工作表中有这些数据
Col A Col B Col C
1 A angry birds, gaming
2 B nirvana,rock,band
Run Code Online (Sandbox Code Playgroud)
我想要做的是拆分第三列中的逗号分隔条目并插入新行,如下所示:
Col A Col B Col C
1 A angry birds
1 A gaming
2 B nirvana
2 B rock
2 B band
Run Code Online (Sandbox Code Playgroud)
我相信这可以用 VBA 来完成,但我自己无法弄清楚。
变体使用 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
Run Code Online (Sandbox Code Playgroud)
来源:
结果: