PDA

View Full Version : Solved: Option Explicit



Opv
03-30-2010, 06:40 AM
I've noticed several of the scripts offered as solutions to various issues that include the statement, 'OPTION EXPLICIT," above the Sub name. Can someone please explain to me the purpose of this statement?

I have started experiencing a problem as a result of the statement. The script worked fine as a stand-alone script. However, when I added new subroutines, I start receiving an error related to the Option Explicit line being after the End Sub statement for the previous subroutine.

I was hoping to be able to keep all my Module-related subroutines in one Module. Is there a way to do this and avoid the problem described above?

Thanks,

Opv

GTO
03-30-2010, 06:57 AM
You only need Option Explicit expressed once, at the top of each module. In short, its inclusion tells Excel to make variable declarations non-optional.

I do not think you'll find many who would disagree with the use of Option Explicit being a good idea. You should always use it. It will save you time in the long run, as you will quickly catch mis-spelled variables. It also helps you get into the good habit of assigning variables to specific types, such as Long, String, etc

Mark

Opv
03-30-2010, 07:02 AM
You only need Option Explicit expressed once, at the top of each module. In short, its inclusion tells Excel to make variable declarations non-optional.

I do not think you'll find many who would disagree with the use of Option Explicit being a good idea. You should always use it. It will save you time in the long run, as you will quickly catch mis-spelled variables. It also helps you get into the good habit of assigning variables to specific types, such as Long, String, etc

Mark

Thanks. That's simple enough to fix. I appreciate the help.

Opv

mbarron
03-30-2010, 07:58 AM
To have all newly created modules have the Option Explicit by default go to Tools/Options... on the Editor tab, check the Require Variable Declaration.

Opv
03-30-2010, 08:04 AM
To have all newly created modules have the Option Explicit by default go to Tools/Options... on the Editor tab, check the Require Variable Declaration.

I just made that change. Thanks!

Opv

sconly
04-06-2010, 02:46 AM
Just a quick follow up question. If the Option Explicit statement is only in one module does it apply to the whole project or just that one module?

thanks

Bob Phillips
04-06-2010, 02:53 AM
Just the one module. It has to be added to each module to affect all, but it is nbetter to set the default as explained earlier.