Looking for Macro to insert rows

AV_Geek

New Member
Joined
Jan 23, 2022
Messages
32
Office Version
  1. 365
Platform
  1. MacOS
I'm looking for a macro in insert rows. I'm trying to insert the following:

1. One row above Row 1
2. Three rows below Row 1
3. Three Rows below Row 51.

I tried this, but SOMETIMES, not always, it just inserts 5 rows above Row 1


VBA Code:
Sub MarchInsertRows1()
Worksheets("Weekdays").Activate
    Rows("1:1").Select
    Rows("2:4").Select
    Rows("52:54").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Worksheets("Buttons").Activate
End Sub

So then I broke it down into 3 Macros, thinking that it was getting confused with going above Row 1 and then again above Row 2

VBA Code:
Sub MarchInsertRows1()
Worksheets("Weekdays").Activate
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Worksheets("Buttons").Activate
End Sub
Sub MarchInsertRows35()
Worksheets("Weekdays").Activate
    Rows("3:5").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Worksheets("Buttons").Activate
End Sub
Sub MarchInsertRows5658()
Worksheets("Weekdays").Activate
    Rows("56:58").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Worksheets("Buttons").Activate
End Sub

Sub MarchInsertRows()
Call MarchInsertRows1
Call MarchInsertRows35
CallMarchInsertRows5658
End Sub

The idea behind this was to let it finish inserting the first group before inserting the second, but it didn't work. It still works one way sometimes and the other way others.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You can use one of these, both work:
Code:
Sub MarchInsertRowsAll_1()
    Worksheets("Weekdays").Activate
    Rows("52:54").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Rows("2:4").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Worksheets("Buttons").Activate
End Sub
Code:
Sub MarchInsertRowsAll_2()
    Worksheets("Weekdays").Activate
    Range("1:1,2:4,52:54").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Worksheets("Buttons").Activate
End Sub
Also note the different order of row insertment.
 
Last edited:
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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