Option Explicit - To use or not.

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi all, I've been working on a new workbook for my wife to record her sales and expenses throughout the year. The VBA elements are pretty basic and ultimately the workbook is only going to be used by her.

I've been doing the final testing on all the forms and code to tidy it all up and remove any test code used whilst building it and just noticed I have missed off Option Explicit on most of the modules, question is do I really need to use it as the code all works fine without it.

I know why is used and may just add it in as I go through the various modules anyway but just wanted to get your thoughts on using it or not.

Thanks Paul
 
That is only true if using arrays, and not really surprising.
Your #2 makes no sense to me. The fact that you are using Option Explicit can't really make you less likely to spot the same typo.
#3 I agree with your premise but not your example. Using i as a variable name doesn't make it clear to me at all what the variable is for, but I don't see how that relates to this discussion anyway. Option Explicit only makes you declare variables. It doesn't make you give them a more specific type than Variant, nor does it help you determine what they are for particularly.
#4 I don't see why you can't do both.
#5 may well be true for you, but doesn't make it a good general practice in my opinion
#6 Excel doesn't define anything sensibly. Everything just becomes a Variant. You can do that yourself just by using Dim. ;)

Having said all that, in a situation where the code is already written, tested, and working, I don't see much point in going back and adding it in. :)
I pleased to see I have stimulated some discussion:
In response:
Your #2 makes no sense to me. The fact that you are using Option Explicit can't really make you less likely to spot the same typo. The reason not using option explicit makes you more likely to spot a typo error, is simply because you get more practice at debugging the sort of error that occurs because of the typo
#3 I agree with your premise but not your example. You are absolutely correct !

Using i as a variable name doesn't make it clear to me at all what the variable is for, The VBA statement I posted does make the use of the variable very clear it is just a loop index, what I should have posted is:

For Loopindex = 1 to 100


but I don't see how that relates to this discussion anyway. Option Explicit only makes you declare variables. It doesn't make you give them a more specific type than Variant, nor does it help you determine what they are for particularly. Which is exactly my point declaring variables doesn’t really help with understanding the code, especially when they are mostly variants or ranges. VBA has very limited variable types so declaring something as Boolean, integer, range, etc doesn’t usually help very much with understanding the code, since the use of the variable usually makes the type obvious anyway. I find my code where I only declare things that have to be declared . e.g objects, dynamic arrays etc is much clearer because the declarations stand out and aren’t lost in a long list of declarations

#4 I don't see why you can't do both. You can but it will take more time and I think the time is better spent writing comments and sensible names
#5 may well be true for you, but doesn't make it a good general practice in my opinion, Taking great care in typing variable names must be good practice!!
#6 Excel doesn't define anything sensibly. Everything just becomes a Variant. You can do that yourself just by using Dim. But that really would be a waste of time!!
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I think that may be the first time I have ever seen a programmer come out AGAINST using Option Explicit...

However, my biggest problem is I am not a great typist, so I am very prone to typos. And I can look at it all day long and not see it.
When I make a typo, it tells me that I have an "undeclared variable", which tells me immediately I made a mistake.

So for me (and I imagine a lot of other "less than perfect" programmers), it has been very helpful over the years.
I am quite surprised to be the first you have seen. I have been programming for over 50 years so I leant to code when there was no help at all, so I don't find option explicit helpful, just a pain because it requires me to type another line of code. I can see it is useful for you and others.
 
Upvote 0
Declaring variables is not about understanding code though.
 
Upvote 0
I am quite surprised to be the first you have seen. I have been programming for over 50 years so I leant to code when there was no help at all, so I don't find option explicit helpful, just a pain because it requires me to type another line of code. I can see it is useful for you and others.
I think that explains a lot. A very experienced programmer (50+ years) who was raised in a very different time probably has already developed (hopefully) good techniques and practices. So it may not be as helpful for someone like you (especially if you are enamored to older methods).

However, for newer programmers who don't have that foundation yet (or for people like me who are really bad typists!), it definitely is beneficial!

You just need to keep in mind that most people who are asking those sort of questions here are probably newer programmers (know thy audience!).
 
Upvote 0
The former isn't important either, since VBA has very limited data types, and most code works perfectly ok with the VBA defaults. That is exactly my point, why waste time and space cluttering up your code with unnecessary lines of declarations!!
 
Upvote 0
The former isn't important either, since VBA has very limited data types, and most code works perfectly ok with the VBA defaults. That is exactly my point, why waste time and space cluttering up your code with unnecessary lines of declarations!!
For one thing, to prevent trying to assign illegal values to a variable.

For example, if I have a variable named "num" and I only want numbers to be assigned to it, I can declare it like this:
VBA Code:
Dim num as Long

Then, if at some point along the way, if someone (or some process) tried to assign a text value to the variable, i.e.
VBA Code:
num = "test"
I would get a "Type mismatch" error alerting me to the fact.

If you don't declare, it is set to Variant, which will allow text values to be assigned to it.

Of course, in this example, it may be obvious when making a hard-coded assignment like that right in the code, but the code could actually be looping through a range of cells and trying to assign those.
So something like that would not be obvious just by looking at the code.
 
Upvote 0
So you don’t type any variables in your code at all?
 
Upvote 0
So you don’t type any variables in your code at all?
I do make declarations, specifically for objects, dynamic arrays and for things like Joe's example above in post 18, i.e. I make a choice of what I declare, which means if there is a declaration at the top of my code it is there for specific reason which makes it stand out. What I don't do is declare anything where the VBA default works and there is no other reason to declare the variable
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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