Jon von der Heyden
MrExcel MVP, Moderator
- Joined
- Apr 6, 2004
- Messages
- 10,910
- Office Version
- 365
- Platform
- Windows
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.I really really know it should be used, but trying to do something quickly having to declare all variables becomes a chore.
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
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
At least that's my thinking... Perhaps there is still good reason to declare variable?