Option Explicit

In a strongly typed langauge you must declare the type of each object and variable you create.
VBA is not a "strongly typed" programming language.
This means you can get away with not explicitly declaring the data type of your variables.
This is very bad practice and leads to code that is slow and unreadable.
Explicitly declaring all your variables will make your programs will run faster and use less memory
There is an option that can force you to explicitly declare the datatype of each variable and this should be switched on.

Option Explicit 


Check You Options

Select (Tools > Options)(Editor tab, "Require Variable Declaration") and make sure it is checked.
Selecting this check box will automatically add the statement Option Explicit to any new modules (not existing ones).
This ensures that a program will not run if it contains any variables that have not been explicitly declared.
Changing this option will not affect any existing modules.


 


Compile-Time Error

Always include Option Explicit the top of all your code modules.
When the Require Variable Declaration check box is ticked you will see the following compile-time error when it finds a variable that has not been declared.


 


Unpredictable Results

If you do not use Option Explicit it can lead to unpredictable results and run-time errors.
When you do not explicitly declare your variables anything that is not recognised as a keyword, statement or function is assumed to be a variable.
Therefore if you accidently mis-spell a variable a new one will be created causing you some odd results.



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext