MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 23rd, 2003, 09:06 AM   #1
nehpets12
 
Join Date: Feb 2002
Posts: 379
Default Option Explicit how to turn it off

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.
nehpets12 is offline   Reply With Quote
Old May 23rd, 2003, 09:11 AM   #2
phantom1975
MrExcel MVP
 
phantom1975's Avatar
 
Join Date: Jun 2002
Location: Omaha, Nebraska
Posts: 3,895
Default Re: Option Explicit how to turn it off

REQUIRE VARIABLE DELCARATION in the OPTIONS menu in VB.
__________________
Silly Billy was here....

***************** EXCEL/VB NEWBIES ARE MY FAVORITE! *****************
phantom1975 is offline   Reply With Quote
Old May 23rd, 2003, 09:21 AM   #3
Tom Urtis
MrExcel MVP
 
Tom Urtis's Avatar
 
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
Default Re: Option Explicit how to turn it off

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.
__________________
Tom Urtis
Tom Urtis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 03:47 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.