Let's assume a simple Excel spreadsheet with two columns A and B, where in the B column there are comma separated values. I need a VBA function to split these values each one in new rows starting from the row just below the cell that contains them. Here an example:
PRE
Column A Column B
AAAAA this,is,a,test
BBBBB other,values
CCCCC 1,2,3,4
POST
Column A Column B
AAAAA
this
is
a
test
BBBBB
other
values
CCCCC
1
2
3
4
I found this question that helped me: Split text in cells at line breaks and modified its solution in this way:
Sub SplitIt()
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, ",") <> 0 Then
tmpArr = Split(Cell, ",")
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
but it do not move down the column B values. Is there a way to do it?