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.
:-?
 
Following on from Denis, IMO an advantage of not using the Option Base is that you might use the array from position 1 to n but if position 0 is available then you can use it just like another variable - very handy for swapping values between positions within arrays.

Just my opinion...
Andrew
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Thanks for your thoughts on that chaps, appreciated :)

I took an introductory course in Visual Basic a couple of years ago but I couldn't progress at the time, so got a job which led me to another which led me to another sat in front of Excel for most of the day, once I started to learn formulas it led me to macros which got me to VBA...

The whole point being, I was always taught to start a new project with Option Explicit and Option Base 1 (the reasons for both was also drilled into us).

It's only recently that I've seen (or noticed) them used in VBA and it got me thinking... But, now I have answers and more to think about...

:)
 
Upvote 0

Forum statistics

Threads
1,225,335
Messages
6,184,335
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