Need help with code to add new last line

rob737

Board Regular
Joined
May 12, 2015
Messages
129
Hi Forum
Hope all is well. I need some help with debugging a Macro Module.

I downloaded an example spreadsheet from the internet for project planning, and it works great with one exception, as follows:


Sheet1 has two Buttons as follows:


  1. Button1 (Label: Insert New Line) - This button enters a new line and copies all the formula etc. and this works fine. You can select any cell and the button adds a line directly below it.
  2. Button2 (Label: Insert New Line at End) – When I select this option I get the dreaded run time error 1004 Reference isn’t valid.



Code as follows

Sub InsertNewLine()
Rows(ActiveCell.Row).Select
Selection.Copy
Selection.Insert Shift:=xlDown
End Sub​
Sub GotoLastCellAndInsertLine()
Application.Goto Reference:="LastRow"
Call InsertNewLine
End Sub​
Private Sub BtnInsertNewLine_Click()
Call InsertNewLine
End Sub​
Private Sub BtnInsertNewLineAtEnd_Click()
Call GotoLastCellAndInsertLine
End Sub​

The debugger stops on Application.Goto Reference:="LastRow". Do I need to define “LastRow” somehow?

As always any help would be greatly appreciated.

Many thanks
Best Regards
Rob
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe....

Code:
Sub InsertNewLine()
    With Rows(ActiveCell.Row)
        .Copy
        .Insert Shift:=xlDown
    End With
    Application.CutCopyMode = False
End Sub
Code:
Sub GotoLastCellAndInsertLine()
    Application.Goto Range("A" & Rows.Count).End(xlUp)
    Call InsertNewLine
End Sub
Code:
Private Sub BtnInsertNewLine_Click()
    Call InsertNewLine
End Sub
Code:
Private Sub BtnInsertNewLineAtEnd_Click()
    Call GotoLastCellAndInsertLine
End Sub
or as I don't see the reason to call the other sub...
Code:
Sub GotoLastCellAndInsertLine()
    With Rows(Range("A" & Rows.Count).End(xlUp).Row)
        .Copy
        .Insert Shift:=xlDown
    End With
    Application.CutCopyMode = False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,480
Members
452,192
Latest member
FengXue

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