VBA: Option Explicit

AD_Taylor

Well-known Member
Joined
May 19, 2011
Messages
687
I know that if you add Option Explicit to the top of a code module, you have to explicitly declare all variables used (which really helps me as I misspell variables a lot)

However if I do misspell one further on down, what happens?
Assuming I have 'On Error Resume Next' activated I won't see the error, so I'll just get very strange results from running the code?

Is there a check I can do, to make sure that all variable definitions are declared?

I.e. Check through a code module and if for example:

undeclaredVar = Cells(1, 1).Value

an undeclared variable is found it would highlight it?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Thanks Peter :)

Shows how much I still have to learn about the VBE Editor! I might know the code but I still don't use the menu items as well as I could.
 
Upvote 0
Option Explicit is a compiler directive - it is interpreted at compile time before the program even starts running. If your program includes a variable which isn't declared - perhaps you misspelled it - then VBA will report this at compile time and your program won't even start executing.

On Error commands are interpreted at run-time - they can only trap error which occur during program execution. If you had an undeclared variable and Option Explicit, your program would never have got as far as the On Error command.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,749
Members
452,940
Latest member
rootytrip

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