FinalRow in VBA not working

BobTriesVBA

New Member
Joined
Jul 21, 2023
Messages
8
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,
Can someone tell me what's going wrong?

1689943314095.png


Starting to learn VBA. I hope I can help you guys back in the future :)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the Board!

It looks like you have "Option Explicit" turned on (which is actually a GOOD thing). That means that you must declare all your variables before using them (which is a "best practice").

So right at the top of your code after your Sub line, just declare your variables like this:
VBA Code:
Dim FinalRow as Long
Dim TotalRow as Long
 
Upvote 1
Solution
Welcome to the Board!

It looks like you have "Option Explicit" turned on (which is actually a GOOD thing). That means that you must declare all your variables before using them (which is a "best practice").

So right at the top of your code after your Sub line, just declare your variables like this:
VBA Code:
Dim FinalRow as Long
Dim TotalRow as Long
Thanks hero! I will keep the "Option Explicit" turned on.
Have a nice day.
 
Upvote 0
Thanks hero! I will keep the "Option Explicit" turned on.
Have a nice day.
You are welcome!

Yes, it is really helpful with debugging. It helps you catch typos in your variables.
For example, if you had those variable declarations:
VBA Code:
Dim FinalRow as Long
Dim TotalRow as Long
But then had a typo like:
Rich (BB code):
TotlaRow = FinalRow + 1
it will tell you that the variable "TotlaRow" is not defined.

And it also helps to ensure that you don't place the wrong data type in a variable, i.e.
if you had something like this:
VBA Code:
Sub MyTest()
    Dim n As Long
    n = "abc"
End Sub
you will get a Run-Time error 13, "type mismatch" when you try to run it, since you are trying to put text in a numeric field.
 
Upvote 0
You are welcome!

Yes, it is really helpful with debugging. It helps you catch typos in your variables.
For example, if you had those variable declarations:
VBA Code:
Dim FinalRow as Long
Dim TotalRow as Long
But then had a typo like:
Rich (BB code):
TotlaRow = FinalRow + 1
it will tell you that the variable "TotlaRow" is not defined.

And it also helps to ensure that you don't place the wrong data type in a variable, i.e.
if you had something like this:
VBA Code:
Sub MyTest()
    Dim n As Long
    n = "abc"
End Sub
you will get a Run-Time error 13, "type mismatch" when you try to run it, since you are trying to put text in a numeric field.
Thanks for all the information. I got 26 chapters left on the 'Microsoft Excel VBA and Macros book'. I think there will be more questions to come in the future :)
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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