VBA to Add Row and Copy Data from Row Below

thelostscott

Board Regular
Joined
May 7, 2010
Messages
226
Hi all,

I'm having trouble creating some code that copies and then deletes values & formulas from a row after I insert a row into a table by using:

Code:
Sub Add_Row()    
    Range("D4").Select
    Selection.End(xlDown).Select
    Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove  
End Sub

This code keeps data in the row below the newly inserted row, but what I want to do is to have the data (Values & Formulas) in the row below copied into the newly inserted row and then deleted from the original row.

Any ideas? (I hope that made sense!)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Code:
With Range("D4").End(xlDown)
    With .EntireRow
        .Insert
        .Offset(-1, 0).Value = .Value
        .ClearContents
    End With
End With
 
Upvote 0
Code:
With Range("D4").End(xlDown)
    With .EntireRow
        .Insert
        .Offset(-1, 0).FormulaR1C1 = .FormulaR1C1
        .ClearContents
    End With
End With
 
Upvote 0
Further alteration needed if possible? Need to keep the formulas in place during the .ClearContents action. Can this be easily done?
 
Upvote 0
What do you mean by "keep the formulas in place during the .ClearContents"?
If you mean "clear only those cells that don't have a formula
Code:
With Range("D4").End(xlDown)
    With .EntireRow
        .Insert
        .Offset(-1, 0).FormulaR1C1 = .FormulaR1C1
        On Error Resume Next
        .SpecialCells(xlCellTypeConstants).ClearContents
        On Error Goto 0
    End With
End With
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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