Excel从列到列的映射

时间:2017-12-27 07:43:20

标签: excel excel-vba excel-formula vba

我在Excel中有以下格式的数据: 输入:

Col1       | Col2
A1;A2;A3;A4|B1;B2;B3;B4
A5;A6      | B5;B6
A7;A8      | B7;B8

我希望数据在后续格式中,即输出

Col1 | Col2
A1 |B1
A2 |B2
A3 |B3
A4 |B4
A5 |B5
A6 |B6
A7 |B7
A8 |B8

我尝试了很多,但没有找到任何有关如何构建此问题的解决方案或在excel中执行此操作。 每个帮助/指南/链接将受到高度赞赏。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

Sub Test()

    Dim arr1(), arr2(), split1, split2
    Dim rng As Range, rngRow As Range
    Dim x As Integer, z As Integer

    'Assuming data starts from A1 cell
    Set rng = Range("A1").CurrentRegion

    For Each rngRow In rng.Rows
        split1 = Split(rngRow.Cells(1), ";")
        split2 = Split(rngRow.Cells(2), ";")
        For x = LBound(split1) To UBound(split1)
            z = z + 1
            ReDim Preserve arr1(1 To z)
            ReDim Preserve arr2(1 To z)
            arr1(z) = split1(x)
            arr2(z) = split2(x)
        Next
    Next

    Range("D1").Resize(UBound(arr1), 1).Value = Application.Transpose(arr1)
    Range("E1").Resize(UBound(arr2), 1).Value = Application.Transpose(arr2)


End Sub