创建excel宏以将单元格中的元素映射到另一个单元格

时间:2013-06-06 19:09:41

标签: excel vba excel-vba

好的,所以我有一堆表格格式的数据:

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

我不熟悉使用宏或使用VBA,因此可以创建一个宏来单独映射B列到A列的内容,如下所示:

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

我在网上看过一堆VBA教程,看不到这样的内容。

1 个答案:

答案 0 :(得分:3)

尝试以下代码:

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

enter image description here