Excel 拆分字段内容并复制行

Excel 拆分字段内容并复制行,excel,vba,Excel,Vba,我有一个Excel电子表格,其中有一列可能有许多值,这些值由分号分隔,例如value1;价值2;价值3。我需要做的是为每个值复制整行,每行只有一个值 例如: value1;value2;value3,abc,100 value4;value5,xyz,200 value6,def,300 结果应该是这样的: value1,abc,100 value2,abc,100 value3,abc,100 value4,xyz,200 value5,xyz,200 value6,def,300 您可以

我有一个Excel电子表格,其中有一列可能有许多值,这些值由分号分隔,例如value1;价值2;价值3。我需要做的是为每个值复制整行,每行只有一个值

例如:

value1;value2;value3,abc,100
value4;value5,xyz,200
value6,def,300
结果应该是这样的:

value1,abc,100
value2,abc,100
value3,abc,100
value4,xyz,200
value5,xyz,200
value6,def,300

您可以使用下面的代码将数据拆分并写入不同的工作表

工作表1包含输入,工作表2包含您要求的输出

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim x As Integer
Dim y As Integer

i = 1  'Row
j = 1  'Col

'Destination Row & Col
x = 1
y = 1

While (Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value) <> "")
    Dim CellValue1 As String
    Dim CellValue2 As String
    Dim CellValue3 As String
    Dim ValArray() As String
    Dim arrayLength As Integer

    CellValue1 = Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value)
    CellValue2 = Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, (j + 1)).Value)
    CellValue3 = Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, (j + 2)).Value)
    ValArray = Split(CellValue1, ";")
    arrayLength = UBound(ValArray, 1) - LBound(ValArray, 1) + 1

    k = 0
    While (k < arrayLength)
        'MsgBox ((ValArray(k) & CellValue2 & CellValue3))
        ThisWorkbook.Sheets("Sheet2").Cells(x, y).Value = ValArray(k)
        y = y + 1
        ThisWorkbook.Sheets("Sheet2").Cells(x, y).Value = CellValue2
        y = y + 1
        ThisWorkbook.Sheets("Sheet2").Cells(x, y).Value = CellValue3
        x = x + 1
        y = 1
        k = k + 1
    Wend
    i = i + 1
Wend
Dim i作为整数
作为整数的Dim j
将k变为整数
作为整数的Dim x
Dim y作为整数
i=1'排
j=1'柱
'目的地行和列
x=1
y=1
而(修剪(ThisWorkbook.Sheets(“Sheet1”).单元格(i,j.Value)”)
Dim CellValue1作为字符串
Dim CellValue2作为字符串
Dim CellValue3作为字符串
Dim ValArray()作为字符串
Dim arrayLength为整数
CellValue1=修剪(ThisWorkbook.Sheets(“Sheet1”).单元格(i,j).Value)
CellValue2=修剪(ThisWorkbook.Sheets(“Sheet1”)。单元格(i,(j+1))。值)
CellValue3=修剪(ThisWorkbook.Sheets(“Sheet1”)。单元格(i,(j+2))。值)
ValArray=Split(CellValue1,“;”)
arrayLength=UBound(ValArray,1)-LBound(ValArray,1)+1
k=0
While(k
您可以使用下面的代码将数据拆分并写入不同的工作表

工作表1包含输入,工作表2包含您要求的输出

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim x As Integer
Dim y As Integer

i = 1  'Row
j = 1  'Col

'Destination Row & Col
x = 1
y = 1

While (Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value) <> "")
    Dim CellValue1 As String
    Dim CellValue2 As String
    Dim CellValue3 As String
    Dim ValArray() As String
    Dim arrayLength As Integer

    CellValue1 = Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value)
    CellValue2 = Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, (j + 1)).Value)
    CellValue3 = Trim(ThisWorkbook.Sheets("Sheet1").Cells(i, (j + 2)).Value)
    ValArray = Split(CellValue1, ";")
    arrayLength = UBound(ValArray, 1) - LBound(ValArray, 1) + 1

    k = 0
    While (k < arrayLength)
        'MsgBox ((ValArray(k) & CellValue2 & CellValue3))
        ThisWorkbook.Sheets("Sheet2").Cells(x, y).Value = ValArray(k)
        y = y + 1
        ThisWorkbook.Sheets("Sheet2").Cells(x, y).Value = CellValue2
        y = y + 1
        ThisWorkbook.Sheets("Sheet2").Cells(x, y).Value = CellValue3
        x = x + 1
        y = 1
        k = k + 1
    Wend
    i = i + 1
Wend
Dim i作为整数
作为整数的Dim j
将k变为整数
作为整数的Dim x
Dim y作为整数
i=1'排
j=1'柱
'目的地行和列
x=1
y=1
而(修剪(ThisWorkbook.Sheets(“Sheet1”).单元格(i,j.Value)”)
Dim CellValue1作为字符串
Dim CellValue2作为字符串
Dim CellValue3作为字符串
Dim ValArray()作为字符串
Dim arrayLength为整数
CellValue1=修剪(ThisWorkbook.Sheets(“Sheet1”).单元格(i,j).Value)
CellValue2=修剪(ThisWorkbook.Sheets(“Sheet1”)。单元格(i,(j+1))。值)
CellValue3=修剪(ThisWorkbook.Sheets(“Sheet1”)。单元格(i,(j+2))。值)
ValArray=Split(CellValue1,“;”)
arrayLength=UBound(ValArray,1)-LBound(ValArray,1)+1
k=0
While(k
在列A中包含数据此宏:

Sub Byron()
    Dim r As Range, K As Long, v As String
    K = 1
    For Each r In Intersect(Range("A:A"), ActiveSheet.UsedRange)
        v = r.Value
        p1 = Mid(v, 1, InStr(1, v, ",") - 1)
        p2 = Mid(v, InStr(1, v, ","))
        ary = Split(p1, ";")
        For Each a In ary
            Cells(K, 2).Value = a & p2
            K = K + 1
        Next a
    Next r
End Sub
将结果放入列B


(这只是将拜伦的评论翻译成VBA)

在列a中有数据此宏:

Sub Byron()
    Dim r As Range, K As Long, v As String
    K = 1
    For Each r In Intersect(Range("A:A"), ActiveSheet.UsedRange)
        v = r.Value
        p1 = Mid(v, 1, InStr(1, v, ",") - 1)
        p2 = Mid(v, InStr(1, v, ","))
        ary = Split(p1, ";")
        For Each a In ary
            Cells(K, 2).Value = a & p2
            K = K + 1
        Next a
    Next r
End Sub
将结果放入列B


(这只是将拜伦的评论翻译成VBA)

看看VBA和变量数组。首先在
上拆分,限制为
2
。然后在
上拆分该数组中的第0项没有限制。迭代这些结果,并将它们连接回第一次拆分后的第1项。如果迭代中有多个项目,则可以添加行。查看VBA和变量数组。首先在
上拆分,限制为
2
。然后在
上拆分该数组中的第0项没有限制。迭代这些结果,并将它们连接回第一次拆分后的第1项。如果迭代中有多个项,则可以添加行。