For line with Debug.Print
everything works fine, meaning Option Explicit
catches an error and displays a pop-up about an undefined variable.
However, if I remove the Debug.Print
line, which I added to see if Option Explicit
works at all, code successfully executes. Compile is successful as well. It should not be, as there is a typo, "vDailBuys" instead of "vDailyBuys" in the 2nd ReDim
statement.
Why isn't this typo detected?
I use the 2nd ReDim
to clear array contents, nut sure if it is recommended way, but it works like a charm.
Option Explicit
Option Base 1
Sub test()
Dim vDailyBuys() As Variant
ReDim vDailyBuys(1, 10)
ReDim vDailBuys(1, UBound(vDailyBuys, 2)) As Variant
Debug.Print abcde
End Sub
Explanation: Because it is so easy to forget to declare a variable, or so easy to misspell a variable's name while coding, thereby inadvertently creating an undeclared variable, visual Basic provides a way that prevents you from entering undeclared variable in your code.
If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur. No initial value is given to the variable: This error commonly occurs when the variable is declared, but not initialized. It means that the variable is created but no value is given to it.
Undeclared − It occurs when a variable which hasn't been declared using var, let or const is being tried to access. Undefined − It occurs when a variable has been declared using var, let or const but isn't given a value.
Undeclared variable means that the variable does not exist in the program at all.
If you try to use an undeclared variable name, an error occurs at compile time. The Option Explicit Off statement allows implicit declaration of variables. If used, the Option Explicit statement must appear in a file before any other source code statements. Setting Option Explicit to Off is generally not a good practice.
If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Deftype statement. Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.
If used, the Option Explicit statement must appear in a module before any procedures. When Option Explicit appears in a module, you must explicitly declare all variables by using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs at compile time.
Option Explicit in VBA is used to make defining variables mandatory while writing codes. It is possible that the developer may misspell a variable or forget to define a variable in his code. The code will not show an error and it will run but we will get a result.
Very simply, with Redim vDailBuys
you've declared a new variable vDailBuys
, instead of your intended resizing of the original vDailyBuys
.
From MSDN - Redim Statement: while "the ReDim statement is used to size or resize a dynamic array that has already been formally declared," it "acts as a declarative statement if the variable it declares doesn't exist at module level or procedure level." (emphasis mine).
Note that the same article advocates only using Redim to redimension arrays. Though it can be a declarative statement, as it is unintentionally in your case, it can cause conflicts that Option Explicit
will not flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With