如何将带有定界值的行分隔为单独的行?

时间:2019-02-07 11:33:10

标签: excel vba

在Excel中,我有多个带有分隔数据的列,需要将其拆分为新行。有些列具有单个值,需要将其复制到新行。我只能在网上看到带有分隔数据的单列示例,因此不确定如何将解决方案应用于包含分隔数据的多列。

我需要为每个分隔项创建单独的行,其中多个列包含分隔列表

我所拥有的:

enter image description here

我需要什么:

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是有关如何实现此目标的简单示例:

Sub Test()

Dim MyArray1() As String, MyArray2() As String
Dim X As Long, Y As Long, Z As Long

Z = 7
For X = 2 To 4
    MyArray1() = Split(Replace(Cells(X, 2).Value, " ", ""), ",")
    MyArray2() = Split(Replace(Cells(X, 3).Value, " ", ""), ",")
    For Y = LBound(MyArray1) To UBound(MyArray1)
        Cells(Z, 1).Value = Cells(X, 1).Value
        Cells(Z, 2).Value = MyArray1(Y)
        Cells(Z, 3).Value = MyArray2(Y)
        Cells(Z, 4).Value = Cells(X, 4).Value
        Z = Z + 1
    Next Y
Next X

End Sub

下面的输出

enter image description here