创build一个Excelmacros以将单元格中的元素映射到另一个单元格

好吧,我有这样的表格格式的一堆数据:

|A | B | ------------------------ 1 |102 | a, b, d, f, g | ------------------------ 2 |104 | a, c, e | ------------------------ 

我是使用macros或使用VBA的新手,所以可以创build一个macros来单独映射B列到A列的内容,如下所示:

  |A | B | --------------- 1 |102 | a | --------------- 2 |102 | b | --------------- 3 |102 | d | --------------- etc.. 

我在网上看了一大堆VBA教程,并没有看到这样的事情。

尝试下面的代码:

 Sub sample() Dim lastRow As Long, i As Long, j As Long lastRow = Range("A" & Rows.Count).End(xlUp).Row For i = 1 To lastRow temp = Split(Cells(i, 2), ",") For j = LBound(temp) To UBound(temp) Cells(Cells(Rows.Count, 3).End(xlUp).Row + 1, 3).Value = Cells(i, 1) Cells(Cells(Rows.Count, 4).End(xlUp).Row + 1, 4).Value = temp(j) Next Next End Sub 

在这里输入图像说明