VBA to ClearContents but keep Formulas!?

thelostscott

Board Regular
Joined
May 7, 2010
Messages
226
Can someone please give me some advice on adding a row whilst keeping formulas in place?

Currently I have the below code:
Code:
Sub Add_Row()
Application.ScreenUpdating = False
 
With Range("D14").End(xlDown)
    With .EntireRow
        .Insert
        .Offset(-1, 0).FormulaR1C1 = .FormulaR1C1
        .ClearContents
    End With
End With

Application.ScreenUpdating = True
End Sub
I need to keep the formulas in place in variable rows and not have them deleted.

Any Ideas?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe like this

Code:
Sub Add_Row()
Application.ScreenUpdating = False
 
With Range("D14").End(xlDown)
    With .EntireRow
        .Copy
        .Insert
        .Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
    End With
End With

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try

Code:
Sub Add_Row()
Application.ScreenUpdating = False
 
With Range("D14").End(xlDown)
    With .EntireRow
        .Copy
        .Insert
        On Error Resume Next
        .SpecialCells(xlCellTypeConstants).ClearContents
        On Error GoTo 0
    End With
End With

Application.ScreenUpdating = True
End Sub
 
Upvote 0
That's done it! Thanks VoG!

Is this also possible to add into the code?

Code:
If Range("D14").End(xlDown).Offset(-1, 0) = "" Then
Exit Sub
Else
What I'm trying to achieve is, if Range("D14").End(xlDown).Offset(-1, 0) is already blank then don't do anything otherwise crack on with the code above?
 
Upvote 0
Try

Code:
Sub Add_Row()
Application.ScreenUpdating = False
If Range("D14").End(xlDown).Offset(-1) <> "" Then
    With Range("D14").End(xlDown)
        With .EntireRow
            .Copy
            .Insert
            On Error Resume Next
            .SpecialCells(xlCellTypeConstants).ClearContents
            On Error GoTo 0
        End With
    End With
End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Just tried that and it still adds on another row to the bottom of the already blank one?

Would it be doing this becase there are formulas in the same row? I just want it to ignore the code if cell reference "D(x)" is empty (x being variable).
 
Upvote 0
Maybe like this

Code:
Sub Add_Row()
Application.ScreenUpdating = False
If Range("D" & Rows.Count).End(xlUp).Offset(-1) <> "" Then
    With Range("D14").End(xlDown)
        With .EntireRow
            .Copy
            .Insert
            On Error Resume Next
            .SpecialCells(xlCellTypeConstants).ClearContents
            On Error GoTo 0
        End With
    End With
End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,949
Latest member
Dupuhini

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