Code from Bill Jelen book gens error

Reverendd2

New Member
Joined
Oct 26, 2009
Messages
5
I've copied and pasted from Bill's book "VBA and Macros for MS Office Excel 2007", tried typing etc. but continue to get an error...

This is supposed to select the final row in the data list...

Error is "Compile Error. Expected Function or Variable."
I thought "FinalRow" was a variable that I am trying to create.
Help?
I'm relearning VBA after a long absence.

Sub FinalRow()

FinalRow = Range("A65536").End(xlUp).Row
TotalRow = FinalRow + 1
Range("A" & FinalRow + 1).Value = "THE END"

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
If that is copied directly from the book, it must just be a typo in the book.


The problem is that it's trying to use a variable that has the same name as the name of the macro. Can't do that..

Just rename the macro to something else..

Rich (BB code):
Sub LastRow()

FinalRow = Range("A65536").End(xlUp).Row
TotalRow = FinalRow + 1
Range("A" & FinalRow + 1).Value = "THE END"

End Sub
 
Upvote 0
Hello and welcome to MrExcel.

You can't name a Sub with the same name as a variable. Try Sub MyFinalRow() instead.
 
Upvote 0
Also declare your variables and it should work:
Code:
[COLOR=blue]Sub[/COLOR] MyFinalRow()
    [COLOR=blue]Dim[/COLOR] FinalRow [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
    [COLOR=blue]Dim[/COLOR] TotalRow [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
 
    FinalRow = Range("A65536").End(xlUp).Row
    TotalRow = FinalRow + 1
    Range("A" & FinalRow + 1).Value = "THE END"
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]

Hope that helps...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,674
Members
449,327
Latest member
John4520

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