问题
While trying to define an Any() function like python's, I found that I couldn't name anything "Any".
Attempting to name a Function, Sub, Const, or variable any
will throw a syntax error and the VBA IDE will highlight it.
I know any not a particularly great name, but why is it throwing a syntax error? The only reason I could think of was that it might be a reserved keyword, but it's not.
回答1:
VBA (Visual Basic for Applications) is not VB.NET, even though they share the same "Visual Basic" monikor and a similar syntax. (The linked documentation is for VB.NET, not VBA.)
VB6 and VBA in Microsoft Office (eg. Access, Excel) handles Any as a reserved word, and it cannot be used as an identifier:
You might also encounter errors if you use a reserved word to name a control, an object, or a variable. The error messages you receive don't necessarily tell you that a reserved word is the cause of the problem.
In VB.NET, however, there is no problem using Any
as a variable name or other identifier:
Dim Any as String = "Hello world!" 'works just fine in VB.NET
回答2:
To get an updated information to this question and comments, Microsoft states on their own page at https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/error-messages/as-any-is-not-supported-in-declare-statements:
The Any data type was used with Declare statements in Visual Basic 6.0 and earlier versions to permit the use of arguments that could contain any type of data. Visual Basic supports overloading, however, and so makes the Any data type obsolete.
来源:https://stackoverflow.com/questions/25316973/why-cant-i-use-the-any-as-a-name-in-vba