Option Explicit

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
This is just a general question about VBA practices, no immediate impact.

Is the only purpose for the line "Option Explicit" at the top of a module to require variables types to be declared within that module? Is there some other purpose for that line that I'm missing?

While I certainly understand that many times it is necessary to declare the variable type, you can certainly agree that it is not always necessary.

Like a simple loop
For i = 1 to 100

If you don't use option explicit and do not declare the type, VBA assumes you mean x is a number (int, long).

or if you say x = "dog" - VBA assumes the variable type is string


A silly comparison would be...

I almost always like ICE in my Glass of Water. But sometimes I don't need or want the Ice. So it would be pretty silly to program my refrigerator to refuse to put water in the glass if I had not put ICE in it first.


Thanks

edited -

And don't come back with "Get your water from the sink then". If that's your response, you missed my point altogether.
:-?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
jonmo

The purpose of Option Explicit is not to do with declaring variable types it's do ensure you declare all variables.

How you type them is irrelevant, obviously apart from how you use them in the subsequent code.

A variable declared without a type will be a Variant.

If in the code it's given a value, say a string, then it will appear in the watch window as Variant/String.
 
Upvote 0
You also don't *have* to include error handlers, document your code, limit the cells users can change, use ISNA instead of ISERROR, etc. but it's good practice.

11 out of 10 programmers (the 10th guy is a good programmer, but not so good at math) would much prefer to inherit code with the variables explicitly declared :-)
 
Upvote 0
Apologies for the incorrect terminology...

Again, I understand the usefulness of declaring variables.

If a "Good Programmer" (which I clearly am not, barely intermediate I think) is already in the practice of declaring variables, why make it Reqired?
 
Upvote 0
jonmo1

It will help even 'good' programmers.

Without Option Explicit it's easy for simple typos to creep into code.

And then you find yourself staring at the code, running it, getting errors and/or incorrect results and then finally realising the problem is a typo.:)
 
Upvote 0
I don't think the defaulting to "Option Implicit"(?) is for the benefit of the "good programmer". Experienced programmers likely flip on the option to require "Option Explicit" shortly after they hide the office assistant.

I think (and this is pure speculation) it's more for the benefit of the user who has never programmed before. If the user's whole exposure to macros is using the macro recorder, s/he won't be concerned (yet) with the advantages/disadvantages of using an integer instead of a variant. S/he would likely find more value in having it "work" than having it be elegant.

Further, things like learning that declaring a variable as an integer doesn't mean it can be "any" integer can be confusing and overwhelming for a noob.
 
Upvote 0
ok, I think I see...

Say I'm using a variable named "jon" in a simple for/next loop..

for jon = 1 to 10

Then I misspell jon in subsequent code, like

If john = 5...

without option explicit -
I would not get an error message, just incorrect results.

But with Option Explicit Turned ON -
I would get an error about a variable not defined, and highlight that line....

So I can see that as a good reason....
Any other reasons?

Not trying to argue, just trying to learn...
 
Upvote 0
Yep, I think you've got it.:)

Though whether or not the misspelling will cause problems really depends on the code.

For example the code may seem to be working perfectly at first but later it could throw errors or produce incorrect results.

And that could be because of the typo with the variable name.

PS Remember this is all just whatifs, conjecture, theory etc.:)
 
Upvote 0
I've got a question thats been muddling around, and this post has reminded me... Well two actually...

1) - Option Explicit - Is this only needed once in a workbook?
Or do you have to declare it in all Sub's / Modules / Worksheets

2) - Option Base - Is this used at all in VBA?

TIA for any answers... :)
 
Upvote 0
1) - Option Explicit - Is this only needed once in a workbook?
Or do you have to declare it in all Sub's / Modules / Worksheets
Put it in every module, but if you change Tools > Options > Editor to Require Variable Declaration, it will be automatic for any new modules that you create.
2) - Option Base - Is this used at all in VBA?
You can use it so that arrays initialise at 1 instead of 0. Generally I don't bother, instead using UBound and LBound to delimit loops when I process array data.

Denis
 
Upvote 0

Forum statistics

Threads
1,225,335
Messages
6,184,332
Members
453,227
Latest member
Slainte

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