VBA code for pasting formulas also pastes values

Trille

New Member
Joined
Apr 26, 2010
Messages
27
Hi!

I have written a code that creates new lines above a given section and should copy the formatting and formulas from the row above. However, the line for copying formulas also carries over values which I would like to avoid.. The sub looks as follows:

Private Sub CommandButton1_Click()
Dim c As Long
For c = 100 To 1 Step -1
If Cells(c + 1, 3) Like "Next section" Then
Cells(c + 1, 1).EntireRow.Insert
Rows(c).Copy
Rows(c).Offset(1, 0).EntireRow.PasteSpecial Paste:=xlPasteFormats
Rows(c).Offset(1, 0).EntireRow.PasteSpecial Paste:=xlPasteFormulas
Application.CutCopyMode = False
End If
Next c
End Sub

Does anyone know how I should amend the sub to clear the values? Alternatively, I'd like to delimit the copying to columns K, L and O.

Any help is greatly appreciated!

/Tristan
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Alternatively, I'd like to delimit the copying to columns K, L and O.
I guess this version should do what you asked:
VBA Code:
Private Sub CommandButton1_Click()
Dim c As Long, CCopy, J As Long
CCopy = Array("K", "L", "O")            '<<< the columns you wish to copy
For c = 100 To 1 Step -1
    If Cells(c + 1, 3) Like "Next section" Then
        Cells(c + 1, 1).EntireRow.Insert
        For J = 0 To UBound(CCopy)
            Cells(c, CCopy(J)).Copy Cells(c + 1, CCopy(J))
        Next J
        Application.CutCopyMode = False
    End If
Next c
End Sub
Try...
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton1_Click()
Dim c As Long
For c = 100 To 1 Step -1
   If Cells(c + 1, 3) Like "Next section" Then
      Cells(c + 1, 1).EntireRow.Insert
      Rows(c).Resize(2).FillDown
      On Error Resume Next
      Rows(c + 1).SpecialCells(xlConstants).ClearContents
      On Error GoTo 0
   End If
Next c
End Sub
 
Upvote 0
Solution
Hi Anthony,

Works well, thank you so much! I just added the command for copying the entire row -> pasting the cell formatting.

Again, I really appreciate your help!
 
Upvote 0
Hi Fluff,

That one also worked well! I really appreciate your help guys, and seeing different methods for solving the problem definitely boosts the learning curve.

Thanks a lot!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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