将Excel中的值拆分为行,同时保留其他列数据

时间:2018-01-22 10:31:04

标签: excel excel-vba vba

我想以非手动方式执行以下操作:

Desired Outcome

我有一个单元格,其中多个值由换行符分隔,我想拆分但是保持其他列中的值不变。

谢谢

1 个答案:

答案 0 :(得分:1)

正如SJR指出的那样,以下内容应该有效:

Sub Foo()
    'working for active sheet
    'copy to the end of sheets collection
    ActiveSheet.Copy After:=Sheets(Sheets.Count)
    Dim tmpArr As Variant
    Dim Cell As Range
    For Each Cell In Range("B1", Range("B2").End(xlDown))
        If InStr(1, Cell, Chr(10)) <> 0 Then
            tmpArr = Split(Cell, Chr(10))

            Cell.EntireRow.Copy
            Cell.Offset(1, 0).Resize(UBound(tmpArr), 1). _
                EntireRow.Insert xlShiftDown

            Cell.Resize(UBound(tmpArr) + 1, 1) = Application.Transpose(tmpArr)
        End If
    Next
    Application.CutCopyMode = False
End Sub