Sometimes, you may need to insert blank rows between each of the existing rows into your Worksheet. Although blank rows are generally bothersome, in some situations, the final formatted version of your report requires them to separate data. This simple macro will help you to do this.
Insert Blank Rows Between Existing Rows
'------------------ Modules ------------------
Sub InsertBlankRows()
'Step 1: Declare your variables.
Dim MyRange As Range
Dim iCounter As Long
'Step 2: Define the target Range.
Set MyRange = Range("A2:A5")
'Step 3: Start reverse looping through the range.
For iCounter = MyRange.Rows.count To 2 Step -1
'Step 4: Insert two blank rows.
MyRange.Rows(iCounter).EntireRow.Insert
'Step 5: Increment the counter down
Next iCounter
End Sub
How This Macro Works
This macro performs a reverse loop through the chosen range using a counter. It starts at the last row of the range inserting one blank rows, and then moves to the previous row in the range. It keeps doing that same insert for every loop, each time increment the counter to the previous row.
- In first Step, We first declare two variables. The first variable is an Range object variable called MyRange. This is an object variable that defines the target range. The other variable is a Long Integer variable called iCounter. This variable serves as an incremental counter.
- In Step 2, the macro fills the MyRange variable with the target range. In this example, we are using
Range(“A2:A5”)
. If your target range is a named range, you could simply enter its name:Range(“MyNamedRange”)
. - In Step 3, the macro sets the parameters for the incremental counter to start at the max count for the range (
MyRange.Rows.Count
) and end at 2 (the second row of the chosen range). Note that we are using theStep -1
qualifier. Because we specifyStep -1
, Excel knows we are going to increment the counter backwards, moving back one increment on each iteration. In all, Step 3 tells Excel to start at the last row of the chosen range, moving backward until it gets to the second row of the range. - When working with a range, you can explicitly call out a specific row in the range by passing a row index number to the Rows collection of the range. For instance,
Range(“A2:A5”).Rows(2)
points to the second row in the range A2:A5. In Step 4, the macro uses the iCounter variable as an index number for the Rows collection of MyRange. This helps pinpoint which exact row the macro is working with in the current loop. The macro then uses the Insert method to insert a new blank row. If you want to insert two blank rows, do this twice. - In Step 5, the macro loops back to increment the counter down.
- When this macro runs, the result is as follows:
How to Use This Macro
Most VBA code should be placed in Standard Modules unless specified.
If you see a comment '------------------ Modules------------------
in the code header that means put the code in a Standard Module. For more information, learn this course: Where should I put the Excel VBA code?
The following steps teach you how to put VBA code into a Standard Module:
- Activate the Visual Basic Editor by pressing ALT + F11.
- Right-click the project/workbook name in the Project Window.
- Choose Insert -> Module.
- Type or paste the code in the newly created module. You will probably need to change the sheet name, the range address, and the save location.
- Click Run button on the Visual Basic Editor toolbar.
- For more information, learn this course: Programming with Excel VBA