Thread: Option Explicit
View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default Option Explicit

Nit-picking point that adds nothing to the point being made.

Bob


"Alan Beban" wrote in message
...
Bob Phillips wrote:

The beauty of this feature is that it can capture typos, because each
variable must be explicitly declared.

Consider this code

For iRowCounter = 1 To 500
If Cells(iRowCounter,"A").Value = "" Then
Cells(iRowCoanter,"B").Value = "X"
End If
Next i

IF you don't have Option Explicit, it will run okay, but not work as the
variable is mis-spelt in the action statement. If you have Option

Explicit,
it will not compile, so you will immediately force you to correct it.

Could
save hours of debugging.

In fact, the above code won't run okay even without Option Explicit,
first because the Next i line will cause the "Invalid Next control
variable reference" error (a compile error), second because the
iRowCoanter line will cause the "Application-defined or object-defined
error" (a runtime error). An example to make the point could be:

For iRowCounter = 1 To 500
If Cells(iRowCounter, "A").Value = "" Then
Cells(iRowCoanter + 1, "B").Value = "X"
End If
Next iRowCounter

Alan Beban