What does this macro code mean?

Closed
nbe9 Posts 3 Registration date Tuesday April 19, 2016 Status Member Last seen April 21, 2016 - Apr 20, 2016 at 10:45 AM
nbe9 Posts 3 Registration date Tuesday April 19, 2016 Status Member Last seen April 21, 2016 - Apr 21, 2016 at 03:38 PM
Hey,

The below macro was posted by another user, aquarelle, and is very useful for inserting copies of rows in an excel sheet. I tried asking them my question directly but haven't received an answer yet. I am trying to understand what the last section of the code does starting at: Range("A1).Select

Here is the code:

Sub InsertRows()
Dim I As Long, J As Integer, Nb As Integer

For I = Range("A65536").End(xlUp).Row To 2 Step -1

Nb = 4

For J = 1 To Nb - 1
Rows(I + J).Insert xlDown
Rows(I).Copy
Rows(I + J).PasteSpecial '
Next

Next

Range("A1").Select
Application.CutCopyMode = False
End Sub

Any help would be much appreciated.
Thanks

3 responses

Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 69
Apr 20, 2016 at 12:01 PM
Hi nbe9,

Please note that Range("A1").Select will take the Cursor to Cell A1 which is the beginning of the sheet. Indeed it is a good practice to place the Cursor at the beginning of the Sheet after the Macro execution has completed that's the reason it has been incorporated in this code.
0
nbe9 Posts 3 Registration date Tuesday April 19, 2016 Status Member Last seen April 21, 2016
Apr 20, 2016 at 05:00 PM
Hey Zohaib R,

Thanks that makes perfect sense! Follow up questions:

To make the macro stop at a specific row should I only change the "Row To 2" number?

To make the macro insert a different number of rows should I only change the "Nb = 4" number?
0
Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 69
Apr 21, 2016 at 01:52 AM
Hi nbe9,

Sorry for the delayed response.

Range ("cellNumber") will not stop a Macro from executing. This only takes the Cursor to the specified location and executes operations on that selected range. There are are other ways of stopping a Macro from execution such as using an InputBox to receive user input before executing further or pressing the Esc key (if the code of the Macro is fairly long).

Yes, you can select a range and then perform the insert operation, it will execute on the selected range only.
0
nbe9 Posts 3 Registration date Tuesday April 19, 2016 Status Member Last seen April 21, 2016
Apr 21, 2016 at 03:38 PM
sorry I wasn't talking about changing the Range ("cellNumber"), I was referring to the part of the code that says

Row To 2 Step -1
0