Option Explicit

  • Thread starter Thread starter Grace
  • Start date Start date
G

Grace

With this new feature, it seems everything must be defined. It just told me
that "i" in my do loop was undefined. How should I dimension this?

Thanks,
Grace
 
Hi Grace

that's the purpose of option explicit (and by the way, its not a new
feature - it must not have been turned on in your previous version)

if i is a counter you can use

dim i as long

cheers
JulieD
 
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.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Bob said:
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
 
Nit-picking point that adds nothing to the point being made.

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top