# Who uses Option Explicit?



## Colin Legg (Sep 3, 2010)

Following on from the Personal.xls poll, I thought it would be interesting to gauge how many VBA'ers on this board use Option Explicit to enforce variable declaration.

I thought there might be a trend relating to how long peeps have been coding in VBA so I've broken the poll into categories depending on VBA experience - I'll leave it to you to decide which experience bucket you belong in. Sorry if the number of choices on the poll is a bit daunting.

When you vote, if you want to post too to explain why you do or don't use it then that'd be interesting.


----------



## TinaP (Sep 3, 2010)

I prefer declaring all my variables just to keep everything neat and tidy.  My programming instructors would be so proud.


----------



## RoryA (Sep 3, 2010)

First thing you should do in the VBE is turn _off_ auto syntax check and turn _on_ required variable declarations.  (second thing is to install MZ Tools of course)


----------



## Atroxell (Sep 3, 2010)

I have learned in the last couple of years that it is far easier to turn on Option Explicit and necessitate variable declaration than it is to search for the malfunction without even a clue as to why something will not work. 

I also like the fact that the result is a little more efficiency in the code.


----------



## Andrew Fergus (Sep 4, 2010)

Definitely use Option Explicit.  Prevents inadvertently miscoding variable names.


----------



## VoG (Sep 4, 2010)

I put myself down as an advanced user who uses Option Explicit. Not sure whether advanced is strictly accurate though...

But I also used it when I was very much a beginner. In fact I think that I learned to do that even before I learned not to Select


----------



## SydneyGeek (Sep 4, 2010)

I use Option Explicit all the time. I'm such a graet tpyist that it's worth my while...
Also, once I set the capitalisation in the variable I always type in lower case after that. Helps catch typos...

Denis


----------



## Andrew Fergus (Sep 4, 2010)

SydneyGeek said:


> Also, once I set the capitalisation in the variable I always type in lower case after that. Helps catch typos...


Either great minds think alike, or fools never differ!  I do exactly the same thing.  If the variable doesn't auto-capitalise after I press Enter, I know I've made a tpyo.

Andrew


----------



## Smitty (Sep 4, 2010)

SydneyGeek said:


> Also, once I set the capitalisation in the variable I always type in lower case after that. Helps catch typos...


 
Ditto 

It's funny how hard it is to get some people to use it - especially some of those who have just "graduated" from the recorder and have never had a need for it ("but my code always worked up until now...")


----------



## DonkeyOte (Sep 4, 2010)

VoG said:


> I put myself down as an advanced user who uses Option Explicit. Not sure whether advanced is strictly accurate though..



Ditto on all fronts though unlike Peter I confess I did not use Option Explicit until around 2 years ago 
_(this despite having worked in default Option Explicit .net prior to that - why ? I've no idea)._

As advised by R I don't use Auto Syntax - I don't think any PC/laptop of mine would last more than a day were I to leave that setting enabled


----------



## Fazza (Sep 5, 2010)

Option explicit should be the default; I don't know why it isn't.


----------



## Jon von der Heyden (Sep 5, 2010)

SydneyGeek said:


> I use Option Explicit all the time. I'm such a graet tpyist that it's worth my while...
> Also, once I set the capitalisation in the variable I always type in lower case after that. Helps catch typos...
> 
> Denis



And I thought I was being smart!  Unfortunately same doesn't work for UDTs, typing in lower converts the type name and all previous references to it to lower case!  Grrr!

I hope plenty more participate in the poll.  It would be nice to see the stats with a larger sample.  Especially like that you have separated advanced, intermediate and new users in the sample, because I have often wondered how many accomplished programmers actually don't force explicit declaration.


----------



## erik.van.geit (Sep 5, 2010)

Using Option Explicit all the time and to complete the variable names without typos using Ctrl+Spacebar

example
type this

```
Dim tzhkzjthkjghdlkfgjfhdslgkjdshg As Single 'or whatever
tz
```
then Ctrl+Spacebar and you'll get the variable at once

kind regards,
Erik


----------



## gardnertoo (Sep 5, 2010)

I voted Intermediate - No.  I know I should use OE, I just forget to.  I'll try to do better, I promise!


----------



## Smitty (Sep 5, 2010)

Just go into the VBE tools and check it.  That way you won't need to worry about forgetting.


----------



## Norie (Sep 5, 2010)

I've got it turned on in all applications where I use VBA.

Mind you I do find myself deleting it quite a lot when all I want to do is some quick testing.

How about compiling?

Do many people just run the code before using Debug>Compile Project?

One handy thing I noticed recently after installing 2010 was that once I'd set Option Explicit on in Excel it was reflected elsewhere.

I thought I'd end up having to set it in each application as I went.

How about IntelliSense?

It's normally pretty handy, for things like completing variable names as Erik pointed out, but I find sometimes I end up declaring Longs as LoadPictureConstants etc..


----------



## Jaafar Tribak (Sep 6, 2010)

I have the _Option Explicit_ set by default but I tend to temporarly remove it if I am doing some quick testing of code that contains API callbacks to avoid cheerful GPFs.


----------



## Sandeep Warrier (Sep 6, 2010)

I'm relatively used to always declaring variables (xept for when I'm too lazy ) ... never thought of using Option Explicit .... programmed a bit in C while in college

When I started VBA, I had some accidental "non-declarations" by using

Dim XYZ, ABC, DEF As Integer..... because I thought this would consider all the variables as integers ... cuz I was used to

Int a, b, c etc.... in C


----------



## SydneyGeek (Sep 6, 2010)

Norie said:


> I've got it turned on in all applications where I use VBA.
> How about compiling?
> 
> Do many people just run the code before using Debug>Compile Project?



That'd be me... I compile once I've made lots of changes, and before setting up the next version of a database on a client machine. Probably should do it more often



Norie said:


> One handy thing I noticed recently after installing 2010 was that once I'd set Option Explicit on in Excel it was reflected elsewhere.
> 
> I thought I'd end up having to set it in each application as I went.



Haven't used 2010 enough to come across that yet, but I like the idea. 



Norie said:


> How about IntelliSense?
> 
> It's normally pretty handy, for things like completing variable names as Erik pointed out, but I find sometimes I end up declaring Longs as LoadPictureConstants etc..



Yeah, you get some weird datatype declarations with Intelli(non)sense but on the whole I find it useful enough to keep it enabled. I like Erik's shortcut though. 

Denis


----------



## yytsunamiyy (Sep 6, 2010)

Voted intermediate-always. Just as Denis said - I'm too bad a typist not to rely on the capitalisation trick, and I had a few nasty surprises with undeclared variables. Variant data type doesn't always give you what you'd expect to get.


----------



## Domski (Sep 6, 2010)

Voted intermediate and always use it (don't really remember ever not).

I use Ctrl+Space all the time to complete my variable names and whether I compile code before running it generally depends how I'm feeling that day.

This is a classic mistake I've seen from a few people who are pretty competent at writing VBA:


```
Dim lngLastRow, lngLoopRow, lngFirstRow As Long
```

Not that using Option Explicit makes any difference to that.

Dom


----------



## RoryA (Sep 6, 2010)

So who voted Advanced - No? Colin (you always vote no)?


----------



## Colin Legg (Sep 7, 2010)

I'm wondering when you going to put your vote against the "What The Heck Are You Talking About" choice, Rory?


----------



## RoryA (Sep 7, 2010)

For once, I think I do though.


----------



## Colin Legg (Sep 7, 2010)

It rubbed against the grain, but I voted yes on this poll. *cough*


----------



## Fazza (Sep 7, 2010)

*minor tangent*

On a minor tangent, I've been caught out in the past typing in directly.

range("whatever").end(xlleft)
instead of
range("whatever").end(xltoleft)

Often enough that I now know to use xltoleft


----------



## schielrn (Sep 8, 2010)

*Re: minor tangent*

I put intermediate, No.  I don't fell that I am quite to the advanced level yet, as I have no clue where to start with API's and that sort of stuff, but I can still program a great program.  And I am just too lazy to set the option to turn it on.  I do comment a lot in my code and basically spell everything out in pseudo code, just neevr really felt the need to have it explicitly on.


----------



## Darren Bartrup (Sep 8, 2010)

Yep, had it on for a few years now and the "auto-syntax interrupt your copy & pasting feature" turned off.

I always forget about the Ctrl+Space bit - probably because it annoys my co-worker, who shouldn't be looking over my shoulder and getting on with his own work.

I didn't know about the variable declaration until about a year ago though, but hardly ever came across that problem as I usually put variables on different lines unless they're X, Y, Z flags, counters, whatevers.


----------



## ZVI (Sep 8, 2010)

Do you remember your first steps in VBA?

For me it was the playing time with VBA code examples given from anywhere.
All was ok until I’ve followed the strong suggestion to use Option Explicit statement as the default VBE setting.
As the result after such setting I’ve spent a lot of time to sort out the break reason of previously working code.

My point of view now – skipping of Option Explicit statement is the *feature* for beginners.
Because without OE they can concentrate on the fast way to achieve the motivation result similar to Hello world program.

Therefore, there is no Option Explicit statement in my suggestions because I know that the code could be added to the working one developed without OE.  
But despite of this the suggested code is developed and debugged on my side with Option Explicit and with all variables declared. 

So, should I vote for "What the heck are you talking about"?


----------



## snowblizz (Sep 9, 2010)

gardnertoo said:


> I voted Intermediate - No.  I know I should use OE, I just forget to.  I'll try to do better, I promise!


Same here.

I really really know it should be used, but trying to do something quickly having to declare all variables becomes a chore. So I can certainly see why people don't use it.


----------



## Jon von der Heyden (Sep 9, 2010)

snowblizz said:


> I really really know it should be used, but trying to do something quickly having to declare all variables becomes a chore.


I use variables if I need to reuse them at various points in my code.  I often see code on the forum where variables are declared, and initialised but only used say once (I am occassionally guilty of this too).  Using With blocks for one-off's can often spare the need for variables.

For instance, I often see:

```
Public Sub NeverEatYellowSnow()
    Dim objDict As Object
    Set objDict = CreateObject("Scripting.Dictionary")
   
    'Some stuff here
    If Not objDict.exists(Something) Then
        objDict.Add Key:=Something, Item:=Something
    End If
   
    'more stuff here
End Sub
```
No need for a variable here, why not:

```
Public Sub NeverEatYellowSnow()
    'Some stuff here
    With CreateObject("Scripting.Dictionary")
        If Not .exists(Something) Then
            .Add Key:=Something, Item:=Something
        End If
    End With
    
    'more stuff here
End Sub
```
I don't see the point of declaring objDict.  The object is destroyed automatically at the end of the sub-routine anyway.

At least that's my thinking...  Perhaps there is still good reason to declare variable?


----------



## RoryA (Sep 9, 2010)

Makes debugging easier - you can inspect the variable in the Locals window. Also makes the code more legible to my mind. That's why I do it, anyway.


----------



## Norie (Sep 9, 2010)

Jon

It may seem like overkill to declare something that is only going to be used once, but it's easier to debug when you have.

You can add the declared variables to the Watch window, drill down through their properties checking values etc as you slowly F8 through the code.


----------



## Jon von der Heyden (Sep 9, 2010)

Yes, that was a good point that I over-looked.  Stil, I suppose it depends on the overall size and complexity of the projecyt involved.  I still think there are many instances where people declare variables where the variable offers little benefit.  I'm not saying it's wrong; I don't see any harm in declaring the variables.


----------



## jeffreybrown (Sep 9, 2010)

Well if I can just say from the point of view of a very novice VBA user, I agree the best option option is to declare variables.

From the very beginning I have always turned on Option Explicit; albeit, not entirley sure what I was doing.

When I asked for help from this board and somebody help with some VBA but they didn't declare the variable it really through me for a loop because I had Option Explicit turned on.

Just my thoughts...


----------



## schielrn (Sep 9, 2010)

Jon von der Heyden said:


> I don't see any harm in declaring the variables.


The file size will be a little bit bigger with each declared variable.


----------



## erik.van.geit (Sep 9, 2010)

schielrn said:


> The file size will be a little bit bigger with each declared variable.


 Perhaps, but the memory used will often be smaller


----------

