VBA to insert rows is not keeping cell contents or formula

Stan101

New Member
Joined
Sep 2, 2016
Messages
26
I have code to insert rows. A dialog pops up asking for the number of new rows required. Those rows are then inserted under. I am using "CopyOrigin" which by all accounts if to copy contents, formulas and formatting but it does not seem to be doing so.

Can anyone offer assistance? I would like to new inserted cells to have the same formatting and contents (and in future possibly formulas)

Also, how would you approach selectively copying contents from row above into these new cells.

EG: after adding 4 new rows after row 5, then copy the contents from B5, f5, and g5 to the new rows.

VBA Code:
Sub InsertRows()


' Adds new blank lines based on the quantity added in the dialog box. It keeps formatting and forumlas.



Dim numRows As Integer

Dim counter As Integer


'Select the current row

   ActiveCell.EntireRow.Select

On Error GoTo Last

   numRows = InputBox("Enter number of rows to insert. Rows will be added above the highlighted row.")


'Keep on inserting rows until we reach the desired number

For counter = 1 To numRows

   Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove

Next counter

  Last: Exit Sub

End Sub
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This might do what you're after
VBA Code:
Sub InsertRows()

    ' Adds new blank lines based on the quantity added in the dialog box. It keeps formatting and forumlas.
    
    Dim numRows     As Long
    Dim raSource    As Range
    Dim bResult     As Boolean
    
    Set raSource = ActiveCell.EntireRow
    numRows = InputBox("Enter number of rows to insert. Rows will be added above the highlighted row.")
    On Error Resume Next
    raSource.Offset(-1, 0).Copy
    bResult = Range(raSource, raSource.Offset(numRows - 1, 0)).EntireRow.Insert(Shift:=xlShiftDown, CopyOrigin:=xlFormatFromLeftOrAbove)
    Application.CutCopyMode = False
    If Not bResult Then
        MsgBox "Inserting rows failed!", vbExclamation
    End If
End Sub
 
Upvote 0
That works. I will play with it further to see what else I can achieve you you have taught me something new. Thanks.
 
Upvote 0
You are welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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