-2

I want to do the following in a non-manual way:

Desired Outcome

I have a cell with multiple values separated by line breaks which I want to split up BUT keep the values in the other columns the same.

Thank you

Community
  • 1
  • 1
  • 1
    Possible duplicate of [Split text in cells at line breaks](https://stackoverflow.com/questions/19851951/split-text-in-cells-at-line-breaks) – SJR Jan 22 '18 at 10:37

1 Answers1

1

As SJR pointed out, the following should work:

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
Xabier
  • 7,587
  • 1
  • 8
  • 20