PRB: Error When Excel VBApp Proc & Implicit Var Have Same Name |
Q113947
In an Excel Visual Basic for Applications module, if you have a procedure and an implicitly declared variable that share the same name, you will get one of two possible errors:
Type-declaration character does not match declared data type.
-or-
Expected function or variable.
Use the Dim statement to explicitly dimension the local variable (ThingOne$ or ThingOne):
Sub ThingOne
End Sub
Sub ThingTwo
Dim ThingOne$ ' Or: Dim ThingOne As Variant
ThingOne$ = "hi"
End Sub
Or add the Option Explicit statement at the beginning of your code module
to force you to explicitly dimension all variables.
This behavior is by design. The local variable ThingOne (or ThingOne$) must
be explicitly declared or you will get an error. Sub procedures within
modules are visible to each other in the Visual Basic, Applications
Edition.
Because ThingOne is visible inside ThingTwo (see the code in the More
Information section below) and Sub and Function procedures may be called
without parameters the reference to ThingOne as a variable is ambiguous.
In the first case, the type char is checked first. The type is determined
to be a String. However, the Sub declaration is equivalent to a function
which has a void return. The $ contradicts this void return, so you get an
error.
In the second case, without the type character, Visual Basic, Applications
Edition checks the return type of the procedure. The return for a Basic Sub
is void so it results in the second error.
This behavior can be avoided altogether by using the Option Explicit
statement.
Sub ThingOne
End Sub
Sub ThingTwo
ThingOne$ = "hi"
End Sub Type-declaration character does not match declared data type.
Sub ThingOne
End Sub
Sub ThingTwo
ThingOne = 4
End Sub Expected function or variable.
Additional query words:
Keywords :
Issue type : kbprb
Technology :
|
Last Reviewed: January 15, 2001 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |