PRB: Error When Excel VBApp Proc & Implicit Var Have Same NameLast reviewed: July 29, 1997Article ID: Q113947 |
The information in this article applies to:
SYMPTOMSIn 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. RESOLUTIONUse 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 SubOr add the Option Explicit statement at the beginning of your code module to force you to explicitly dimension all variables.
STATUSThis 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.
MORE INFORMATION
Steps to Reproduce Behavior
Keywords : kbcode kberrmsg kbprg Version : 1.00 5.00 4.00 Platform : WINDOWS |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |