Excel VBA - Copy Data to Blank Rows After Blanks Rows Inserted Between Data?

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I am looking to see if anyone might have a macro for data that has blank rows inserted between data?

For example beginning with this:
Column_AColumn_BColumn_CColumn_DColum_E
Row_1ALPHAAPPLEABC
Row_2BETAORANGEDEF
Row_3GAMMAKIWIGHI
Row_4DELTACOCONUTJKL
Row_5ECHOLIMEMNO
Row_6FRANKLEMOPQR

<tbody>
</tbody>

I use the following insert blank rows macro to insert three blank rows:
Code:
Sub INSERTBLANKROWS()


Dim j As Long
Dim r As Range


j = InputBox("Enter the number of rows to be inserted")

Set r = Range("A1")
Do While r.Value <> ""
    Set r = r.Offset(1, 0)
    For i = 1 To j
        r.EntireRow.Insert
    Next
Loop


End Sub

And now need to copy the data from each "data row" to the blank rows inserted to end up with the following:
Column_AColumn_BColumn_CColumn_DColum_E
Row_1ALPHAAPPLEABC
Row_2ALPHAAPPLEABC
Row_3ALPHAAPPLEABC
Row_4ALPHAAPPLEABC
Row_5BETAORANGEDEF
Row_6BETAORANGEDEF
Row_7BETAORANGEDEF
Row_8BETAORANGEDEF

<tbody>
</tbody>

What macro would I use to be able to do this?
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub AddFillThreeRows()
  Dim R As Long, LR As Long, LC As Long, HowManyRows As Long
  Application.ScreenUpdating = False
  ' Insert Rows
  HowManyRows = Application.InputBox("Enter the number of rows to be inserted:", Type:=1)
  If Len(HowManyRows) Then
    For R = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
      Rows(R).Resize(HowManyRows).Insert
    Next
    ' Fill Empty Cells
    LR = Cells(Rows.Count, "A").End(xlUp).Row + HowManyRows
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
    With Range("A1", Cells(LR, LC))
      .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
      .Value = .Value
    End With
  End If
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Give this macro a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub AddFillThreeRows()
  Dim R As Long, LR As Long, LC As Long, HowManyRows As Long
  Application.ScreenUpdating = False
  ' Insert Rows
  HowManyRows = Application.InputBox("Enter the number of rows to be inserted:", Type:=1)
  If Len(HowManyRows) Then
    For R = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
      Rows(R).Resize(HowManyRows).Insert
    Next
    ' Fill Empty Cells
    LR = Cells(Rows.Count, "A").End(xlUp).Row + HowManyRows
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
    With Range("A1", Cells(LR, LC))
      .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
      .Value = .Value
    End With
  End If
  Application.ScreenUpdating = True
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
Thanks for the reply although this code did not seem to work all that well for me. I actually ended it up figuring out my own solution to this problem, and have posted my code below:
Code:
Sub copyRowToBelow() 

    Dim rng As Range
    Set rng = Range("A1") ' <~~  Change this

    Do While (rng.Row < 461) 'This value represents total number of rows after first macro created blank rows, so it serves as the upper limit.
        ' Insert a row below the current one
        'rng.Offset(1).Insert

        ' Copy the current row and paste it into the row we just inserted

rng.EntireRow.Copy rng.Offset(1)
rng.EntireRow.Copy rng.Offset(2)

        ' Set the range declaration for 3 rows below the current one
        Set rng = rng.Offset(3)
        
    Loop
   
End Sub
The only thing I am stuck on now is along with the first code that references the input: "j = InputBox("Enter the number of rows to be inserted")"; how to the adjust the code I listed above to dynamically work with the same input, "j". In other words, If "j = 6" is inputted in the first portion of code; then how to update the code listed above to use that same input "j" and copy the data to the same number "j" blanks rows inserted?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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