Option Explicit how to turn it off

nehpets12

Active Member
Joined
Feb 22, 2002
Messages
453
When I record a new macro my first line always comes up with Option Explicit.

I know it is something I turned on a long time ago but I cant remember where it is or how to turn it off.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Before you go without the Option Explicit statement, take a moment to carefully consider that decision.

The term "Option Explicit" means that the VBA code author must define the names of all the variables being used or referred to in the code. Not doing so is called "implicit variable declaration", where you can name a variable without explicitly defining it.

Why should you explicitly declare variables?

Option Explicit at the top of a module forces every variable to be declared. There are several reasons why this is a good idea to maintain:

- Spelling errors are identified.
- Your variables can be properly declared by you, not Excel, to have the appropriate memory and system resources assigned to accommodate them. Excel will assign Variant as the variable default type for undeclared variables, the most inefficient use of resources.
- Confusion is avoided if a variable is the same name as a property or method.

Consider the following code:

Sub ShowData()
FindData = InputBox("Please enter your search term:")
If FineData = "" Then Exit Sub
Range("A1").Value = FindData
End Sub

Without Option Explicit to guard against spelling errors, cell A1 will never have a value returned into it because of the spelling inconsistency between FindData (line 2) and FineData (line 3). The macro will always exit because FineData will always be thought of as empty.

This kind of mistake might be noticeable with a macro this size, but if it is part of a larger procedure these mistakes are hard to identify. Why take the chance?

Keeping Option Explicit as the default statement is highly recommended; it forces better code writing practice, and helps avoid coding errors.
 
Upvote 0
Thank you Tom for the comprehensive explanation of Option Explicit! That is probably the most comprehensive answer I've ever seen, and the question wasn't even asked in this thread! That really helped me, especially with the benefits and the example to illustrate. :biggrin:
 
Upvote 0
Why should you explicitly declare variables?

Option Explicit at the top of a module forces every variable to be declared. There are several reasons why this is a good idea to maintain:

- Spelling errors are identified.

Consider the following code:

Sub ShowData()
FindData = InputBox("Please enter your search term:")
If FineData = "" Then Exit Sub
Range("A1").Value = FindData
End Sub
Just to follow up on Tom's explanation, take a look a the following variable names...

Text10
Text1O
Textl0
TextlO

You are probably asking.... why is he showing us that given the differences are so obvious? Yes, in the font used for this forum, the difference is quite clear, but do this experiment... copy/paste those four lines into a code window (the Immediate Window will do). In the default font used by the VB editor, the differences in appearance are much harder to see, but you can because they are displayed next to each other and isolated from other code. Now try to image any one of them in a long code statement with other code around it. Do you really think you could spot the error when examining the code? If you think you can, then you are only fooling yourself. Using Option Explicit solves the problem for you, if you Dim Text10, then VB will flag your erroneous use of any of the other three. Trust me... that is a HUGE benefit, especially if this code is for use in a production environment where you would want your code to always be producing the correct answer.
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,999
Members
448,541
Latest member
iparraguirre89

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