Copying above row formatting and formula to new row using command button

JoRyBar

New Member
Joined
Aug 19, 2018
Messages
17
Hello VBA Gurus,

I would like to create a new row with a command button and then have VBA codes copy the formatting and certain formulas from the row above into the newly inserted row. I would like to only copy formulas from certain column ranges (i.e. A:B; D:L, U:AA) and leave the rest blank. The code below only inserts the new row, but does not copy the formatting or formulas from the above row:

Private Sub CommandButton1_Click()
Dim rowNum As Integer
On Error Resume Next
rowNum = Application.InputBox(Prompt:="Enter Row Number to Add a Row Above:", _
Title:="Add New Row", Type:=1)
Rows(rowNum & ":" & rowNum).Insert Shift:=xlDown
R = Rows(rowNum & ":" & rowNum).Row
Range("a" & R + 1 & ":b" & R).Formula = Range("a" & R + 1 & ":b" & R).Formula
Range("d" & R + 1 & ":l" & R).Formula = Range("d" & R + 1 & ":l" & R).Formula
Range("u" & R + 1 & ":Aa" & R).Formula = Range("u" & R + 1 & ":Aa" & R).Formula


End Sub

Your assistance is greatly appreciated! Many thanks.

Best Regards,

VBA Novice
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the forum :)
Please enclose code tags around your code (click on # icon above post window and paste code inside the tags that appear)

Try this

Code:
Private Sub CommandButton1_Click()
    Dim rowNum As Long
    On Error Resume Next
    rowNum = Application.InputBox(Prompt:="Enter Row Number to Add a Row Above:", _
        Title:="Add New Row", Type:=1)
    Rows(rowNum).Insert Shift:=xlDown
    If Err.Number > 0 Then GoTo errH
    Range("A" & rowNum - 1).Resize(, 2).Copy Range("A" & rowNum)
    Range("D" & rowNum - 1).Resize(, 9).Copy Range("D" & rowNum)
    Range("U" & rowNum - 1).Resize(, 7).Copy Range("U" & rowNum)
errH:
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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