Excel VBA Option Explicit
Declaration of variables are very important in VBA, Option Explicit makes a user mandatory to declare all the variables before using them, any undefined variable will throw an error while execution of the code, we can write the keyword option explicit or we can enable it for all codes from options by enabling to require variable declaration.
In VBA, it’s all about variables. In order to store data, we need variables with a suitable data type. You can question me why do you need variable when you can directly add the value to the spreadsheet itself. This is mainly because of the multiple users of the workbook. If it is handled by one person, then you can directly reference the value to the sheet itself. By declaring variables, we can make the flexible code to store the data.
What is VBA Option Explicit?
I hope you had come across the blue lined word “Option Explicit” at the top of your module before any macro started in that module.
At the start of learning VBA, I too didn’t understand what that was, and to be very frank, I didn’t even think about this at all. Not only for you or me, but it is also the same for everybody at the start. But we will see the importance of this word now.
“Option Explicit” is our mentor in declaring the variable. By adding this word, it makes the variable declaration a mandatory process.
For example, look at the below code for understanding.
Sub Example1() i = 25 MsgBox i End Sub
If I run this code, we will get the value of a variable “I” in the message box in VBA.
Now I will add the word “Option Explicit” at the very beginning of the VBA code.
Now I will run the code and see what happens. If you are practicing with me, press the F5 key to run the code.
We got a compile error, and it says, “Variable not defined.” We have not declared the variable “i,” but straight away, we have assigned the value to it as 25.
Since we have added the word “Option Explicit,” it forces us to declare the variable compulsorily.
In the above code, the alphabet “i” is undeclared, so we have added the variable controller word “Option Explicit” it prevents us from using the undeclared variables.
The moment you add the word “Option Explicit” at the top of the module, it is applicable for all the macros in that particular module to declare the variables mandatorily.
How to Make the Variable Declaration Mandatory?
If you have added the variable mentor “Option Explicit” manually in your module when you insert the new module, you won’t get this variable mentor by default.
If you think every time you need to add the word “Option Explicit” for all the new modules manually, then you are wrong.
Because we can make this word mandatory in all the modules by doing a simple setting, follow the below steps to adjust the settings.
Step 1: Go to Visual basic editor.
Step 2: Go to TOOLS and click on Options.
Step 3: The moment you click on Options, you will see the below window.
Step 4: Under this window, Go to Editor and tick the option “Require Variable Declaration.”
Step 5: Click on OK to close the window.
From now onwards, whenever you add a new module, it automatically inserts the word “Option Explicit” by default.
Option Explicit is your Saver
Option Explicit helps us in many ways. Right from making the declaration of variable mandatory, it will help us until the execution. Look at the below code.
Sub Example2() Dim CurrentValue As Integer CurentValue = 500 MsgBox CurrentValue End Sub
In the above code, I have declared the variable “CurrentValue” as an integer. In the next line, I have assigned the value of 500 to it. If I run this code, I should get 500 as a result of a message box. But see what happens.
It says “Variable not defined” and highlighted the second line.
If we closely look at the second line, there is a slight spelling mistake. My variable name is “CurrentValue,” but in the second line, I have missed out on one spelling, i.e., “r.” It says “CurrentValue” instead of “CurrentValue.” Since I have made the variable declaration mandatory by adding the word “Option Explicit” in excel VBA, it has highlighted the typo error I have made.
So when we correct the spelling and run the code, we will get the result as follows.
Say hello to the newly appointed variable mentor!!!
This has been a guide to VBA Option Explicit. Here we discuss what option explicit do in VBA and how to make variable declaration mandatory with details. You can learn more about VBA from the following articles –