ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Option Explicit (https://www.excelbanter.com/excel-programming/301259-option-explicit.html)

Grace[_4_]

Option Explicit
 
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



JulieD

Option Explicit
 
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

"Grace" wrote in message
...
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





Norman Jones

Option Explicit
 
Hi Grace,

For a full discussion of Option Explicit go to Chip Pearson's page:
http://www.cpearson.com/excel/DeclaringVariables.htm

In this particular case, Option Explicit is telling you that i has not been
dimmed (dimensioned).
Solution: At the top of the Sub type:
Dim i as long

---
Regards,
Norman


"Grace" wrote in message
...
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





Bob Phillips[_6_]

Option Explicit
 
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)

"Grace" wrote in message
...
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





Alan Beban[_2_]

Option Explicit
 
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

Grace[_4_]

Option Explicit
 
Thanks, Norman, and Julie, and Bob.

Grace

"Norman Jones" wrote in message
...
Hi Grace,

For a full discussion of Option Explicit go to Chip Pearson's page:
http://www.cpearson.com/excel/DeclaringVariables.htm

In this particular case, Option Explicit is telling you that i has not

been
dimmed (dimensioned).
Solution: At the top of the Sub type:
Dim i as long

---
Regards,
Norman


"Grace" wrote in message
...
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







Bob Phillips[_6_]

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





All times are GMT +1. The time now is 02:13 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com