Macro to get data in the below format

Rev T

New Member
Joined
Jun 3, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I have data in the below format -

Col A1Col A2Col A3Col A4Col A5Col A6Col A7Col A8Col A9Col A10Col A11Col A12Col A13Col A14Col A15Col A16Col A17Col A18
ABC1,2,3,4,5,6,7,8DEFGHIJKLMNOPQ

I need it to be this way -

Col A1Col A2Col A3Col A4Col A5Col A6Col A7Col A8Col A9Col A10Col A11Col A12Col A13Col A14Col A15Col A16Col A17Col A18
ABC1DEFGHIJKLMNOPQ
ABC2DEFGHIJKLMNOPQ
ABC3DEFGHIJKLMNOPQ
ABC4DEFGHIJKLMNOPQ
ABC5DEFGHIJKLMNOPQ
ABC6DEFGHIJKLMNOPQ
ABC7DEFGHIJKLMNOPQ
ABC8DEFGHIJKLMNOPQ

Can someone help me out on this, thanks!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Welcome to the Board!

So, is your value in column "A4" really column D on your worksheet?
The columns typically go in A, B, C, D, etc order, and rows go in 1,2,3,4, order.

What cell does the first line of data start in?
Is there a title row, so it is in cell A2, or is it in A1?
 
Upvote 0
So, is your value in column "A4" really column D on your worksheet? - Yes
What cell does the first line of data start in? - A2
Is there a title row, so it is in cell A2, or is it in A1? - Yes, there's a title row and it's in A1
 
Upvote 0
One last question I forgot to ask.
Is there always just one single row of data that you need to do this too, or might there be multiple rows that need to be expanded like this?
 
Upvote 0
There are multiple rows and it can be anywhere between 2 and 100 values separated by a comma in column D
 
Upvote 0
A bit of a "brute force" method, but it works:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long
    Dim r As Long
    Dim arr() As String
    Dim nr As Long
    Dim i As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column D with data
    lr = Cells(Rows.Count, "D").End(xlUp).Row
    
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Get value from column D and split
        arr = Split(Cells(r, "D"), ",")
'       See if we need to insert any rows
        nr = UBound(arr)
        If nr > 0 Then
'           Insert new rows
            Rows(r + 1 & ":" & r + nr).Insert
'           Copy row info to new rows
            Rows(r).Copy Rows(r + 1 & ":" & r + nr)
'           Populate column D
            For i = LBound(arr) To UBound(arr)
                Cells(r + i, "D") = arr(i)
            Next i
        End If
    Next r

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
:)
 
Upvote 0
@Joe4 @Rev T
I would suggest one small change and that is to write arr to the worksheet all at once instead of a cell at a time.

Rich (BB code):
For i = LBound(arr) To UBound(arr)
    Cells(r + i, "D") = arr(i)
Next i
Cells(r, "D").Resize(nr + 1).Value = Application.Transpose(arr)
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top