Who uses Option Explicit?

Do You Use Option Explicit When You Write VBA Code?

  • I'm an advanced VBA user and I use Option Explicit

    Votes: 29 45.3%
  • I'm an intermediate VBA user and I use Option Explicit

    Votes: 22 34.4%
  • I'm a new VBA user and I use Option Explicit

    Votes: 3 4.7%
  • I'm an advanced VBA user and I do not use Option Explicit

    Votes: 1 1.6%
  • I'm an intermediate VBA user and I do not use Option Explicit

    Votes: 5 7.8%
  • I'm a new VBA user and I do not use Option Explicit

    Votes: 1 1.6%
  • What the heck are you talking about?

    Votes: 3 4.7%

  • Total voters
    64
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:
Code:
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:
Code:
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?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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.
 
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.
 
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.
 
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...
 

Forum statistics

Threads
1,223,768
Messages
6,174,408
Members
452,562
Latest member
Himeshwari

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