Macro Button to add a row with existing formulas

JCM

New Member
Joined
Jan 15, 2025
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
What is the code to create a macro button that will insert a new row below that last row with all formatting of the previous rows
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try:
VBA Code:
Sub CopyRow()
    Dim lRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Rows(lRow).Copy Range("A" & lRow + 1)
End Sub
 
  • Like
Reactions: JCM
Upvote 0
Sorry I am having problems getting the button correctly. I will fix that process first and let you know. Until then Thanks for taking the time, it is greatly appreciated.
 
Upvote 0
You would have to create the button first and then assign the macro to it.
 
  • Like
Reactions: JCM
Upvote 0
I set up the button and it did not work, or I thought it did not work then I realized at the bottom of the sheet was a total column and your code kept inserting a row below the total column. lol
So if the last row is 15 and the TOTAL ROW (so labeled in column A row 16) is row 16 how do I get it to insert a new row above the total row with all the formating in row 15?
 
Upvote 0
VBA Code:
Sub CopyRow()
    Dim lRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Rows(lRow).Copy
    Rows(lRow).Insert Shift:=xlDown
End Sub
 
  • Like
Reactions: JCM
Upvote 0
Is it only formatting that needs to come down from the previous row ?
Does the new row need to blank out constants but bring down formulas ?
Wouldn't you better off using an Excel Table ?


@Logit is 1 row out. lRow is the total row.

VBA Code:
Sub CopyRow_Log()
    Dim lRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Rows(lRow - 1).Copy                     ' Copy row 1 row above the total row
    Rows(lRow - 1).Insert Shift:=xlDown     ' Insert above the last data row so the Total will include all data rows
    'Rows(lRow).ClearContents               ' <--- Optional - if you want the last row to be blank
End Sub
 
  • Like
Reactions: JCM
Upvote 0
Alex that worked perfectly. Thank you very much. I will study it to try to understand the logic.
Again Thanks
JCM
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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