Macro: Button click creates new blank row

Cavill

New Member
Joined
Apr 23, 2010
Messages
20
I am new to the VBA side of excel so basically I am a bit of a newbie. I don't know how to code in VBA yet but am attempting to learn.

I am creating a dynamic spreadsheet to record my stock portfolio.

I want to make a macro that creates a new row below Row 1 or above Row 2 (Always a new row in row 2 basically). I want the new row to have the same fill colour used in Row 3 and the same row height, ranging between columns A3-J3.
I also want this new row to include formulas used in cells J3, I3, G3, F3 and D3. Is this possible?

Additionally I want to create a button (I know how to create a button and assign a macro to it already) that is off to the side that is assigned to the macro that creates the new row, however I want the button fixed to its placed position, and doesnt move down as the new rows are created, anyone know how to do this?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Maybe:
Code:
Sub Macro1()
    On Error Resume Next
    Rows("3:3").Copy
    Rows("2:2").Insert Shift:=xlDown
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Here's another version..

Where you data looks like this before running Macro (below):
Excel Workbook
ABCDEFGHIJ
1MyHeader1MyHeader2MyHeader3MyHeader4MyHeader5MyHeader6MyHeader7MyHeader8MyHeader9MyHeader10
2sdfggdf654.001,308.009,879.003,293.00327.00654.004,939.50654.00
3fghfsd12.0035.0065.00456.002,345.00354.003,456.00567.00
Sheet1
Excel 2007
Cell Formulas
RangeFormula
D2=C2*2
F2=E2/3
G2=C2/2
I2=E2/2
J2=D2/2


In a standard Module paste In:
Code:
Sub MyNewRow2()
    Application.ScreenUpdating = False
    Rows("2:2").Insert Shift:=xlDown   'Insert New Blank Row 2
    Rows("3:3").Copy Range("A2")    'Copy dtat from Old row 2 to new row 2
    Range("A2:J2").SpecialCells(xlCellTypeConstants, 23).ClearContents     'Clear all Constants, not formulas
    Range("A2").Select
    Application.ScreenUpdating = True
End Sub

It should Now Look like this:
Excel Workbook
ABCDEFGHIJ
1MyHeader1MyHeader2MyHeader3MyHeader4MyHeader5MyHeader6MyHeader7MyHeader8MyHeader9MyHeader10
2-----
3sdfggdf654.001,308.009,879.003,293.00327.00654.004,939.50654.00
4fghfsd12.0035.0065.00456.002,345.00354.003,456.00567.00
Sheet1
Excel 2007
Cell Formulas
RangeFormula
F2=E2/3
F3=E3/3
G2=C2/2
G3=C3/2
I2=E2/2
I3=E3/2
J2=D2/2
J3=D3/2
D2=C2*2
D3=C3*2
 
Upvote 0
Thanks a lot guys! Machopicho your macro worked, although it copied the cells from row 3 however it copied every cell, where as I only wanted the named ones to be copied, thanks for your help though!

Jim May your macro worked perfectly how I wanted it, thank you very much! However there is one minor fault, if I use the macro once, then use it again straight after to create two new rows, an error occurs it takes me to VB and an error prompt "Run-time error '1004': No cells were found." occurs, any idea why this would be occurring? Not a huge problem if it can't be fixed.

Having 1 more problem with the button I made that has this macro assigned to it. The button moves down the page when new rows are created. I want this button to be fixed in the same spot all the time, even while new rows are created... I tried to order it "Bring to front" but that didn't work.
 
Upvote 0
I just noticed another problem. I have cells at the bottom that sum the above cells. Now with a new row, the cell that sums the cells will not add the cells in the new row that is created. I can't use absolute cell referencing because I need it to basically add all cells from row 2 all the way to the cell right above the cell that sums the above cells.
Currently I only have two cells that total the above cells, again is this possible?
 
Upvote 0
Add these 2 lines (in Red), bepow

Rich (BB code):
Sub MyNewRow2()
    Application.ScreenUpdating = False
    On Error Resume Next
    Rows("2:2").Insert Shift:=xlDown   'Insert New Blank Row 2
    Rows("3:3").Copy Range("A2")    'Copy dtat from Old row 2 to new row 2
    Range("A2:J2").SpecialCells(xlCellTypeConstants, 23).ClearContents     'Clear all Constants, not formulas
    Range("A2").Select
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub

Expand heighth of Row 1 to accomodate the size of your button (drag button so that it is totally inside row 1, not hanging over into row 2. Then with cell A2 the current cell --Freeze
the Window (that way Row 1 always appears on screen (With your button).

Expand your SUM to Include the Headings (Sum B1:B20) -- If Row 1 is text header this shouldn't be a problem.
 
Upvote 0
Wow awesome, no more errors! All problems resolved thanks a lot Jim you are a champion. My portfolio spreadsheet is looking great and functional! :)
 
Upvote 0

Forum statistics

Threads
1,217,361
Messages
6,136,103
Members
449,991
Latest member
IslandofBDA

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