Insert copied rows to every other row

shimmyyaz

New Member
Joined
Dec 9, 2008
Messages
3
I have a spreadsheet that has data in rows 2-21

There is then more data in multiple rows below 22. What I would like to do is copy rows 2-21 and then insert them between each one of the rows below 22

Any ideas how I can do this through a macro? It is a huge spreadsheet and I cannot do this by hand. Thanks in advance for your help.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Are rows 23,25,27, etc. currently blank and you want 2-21 to be pasted to those blank rows?

Or are rows 22,23,24,... currently filled and you want to new rows (with data from 2-21) to be inserted between each of those rows?

In either case, adding a helper column and putting the order that you want in that column, then sorting on that column is one approach.
 
Upvote 0
All of the rows below 22 contain data. I need it to insert rows 2-21 in between each of the other data rows. Also I am trying to avoid the helper column as the spreadsheet is already 14,000 lines.


Before anyone says it I know I will have to divide this up as well because Excel won't handle the 250,000 some odd lines I am going to end up with.
 
Last edited:
Upvote 0
I'm at work and can't work on this right now, but my thought would be to use the helper column to sort only rows2-42 rather than the whole thousands of lines

row 2 would hold 22.5
row 3 23.5
row 4 24.5
...
row 21 41.5
row 22 22
row 23 23
...
row 41 41
Then sort only rows 2-41 on that column.
Delete the helper column,
Insert new, blank rows 2-21
 
Upvote 0
I am not sure how this would work?? How does this insert the desired rows between each of the rest of the lines?
 
Upvote 0
It doesn't insert rows (until the end when blank rows are inserted at the top)
It sorts rows 2,3,4,...,21,22,23,...42 into the order 21,2,22,3,23,4,....,42 and then inserts blank rows above the whole data range.
 
Upvote 0
Does this do what you want?
Code:
With ThisWorkbook.Sheets("Sheet1"): Rem adjust
    With .UsedRange
        With .Cells(1, .Column + .Columns.Count + 2).EntireColumn: Rem helper column outside of used range
        
            Rem fill helper cells
            .Range("A2:A20").Value = Evaluate("=ROW(A2:A20)+19.5")
            .Range("A21:A40").Value = Evaluate("=ROW(A21:A40)")
            
            With Range(.Parent.Range("A2"), .Range("A40"))
                Rem sort on helper column = interlace rows 2-20 with 21-39
                .Sort Key1:=.Cells(1, .Columns.Count), Order1:=xlAscending, Header:=xlNo, _
                    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
                    
                Rem insert new empty range rows 2-20
                .Rows(1).Resize(19, .Columns.Count).Insert shift:=xlDown
            End With
            
            .Delete: Rem done with helper column
        End With
    End With
End With
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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