Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use the "Any" as a name in VBA?

Tags:

excel

vba

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.

like image 568
cheezsteak Avatar asked Aug 14 '14 20:08

cheezsteak


People also ask

Which character is not used as variable name in VBA?

You can't use a space, period (.), exclamation mark (!), or the characters @, &, $, # in the name. Name can't exceed 255 characters in length. Generally, you shouldn't use any names that are the same as the function, statement, method, and intrinsic constant names used in Visual Basic or by the host application.

How do I fix syntax error in VBA?

To do this, click on 'Tools' and then click on 'Options'. In the options dialog box, make sure that the 'Auto Syntax Check' option is enabled. If the 'Auto Syntax Check' option is disabled, VBA will still highlight the line with the syntax error in red, but it will not show the error dialog box.

Which of these is a valid VBA variable name?

Answer: Variable names in Visual Basic are made up of letters (upper and lower case) and digits. The underscore character, "_", is also permitted. Names must not begin with a digit.

Does != Work in VBA?

How Not Equal Works in Excel VBA? VBA Not Equal works exactly opposite to the logic of equal to operator. Equal to operator returns TRUE if the supplied test is satisfied is not, it will return FALSE. So, for example, if you say 10 = 10, it will return TRUE or else FALSE.


1 Answers

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
like image 78
user2864740 Avatar answered Oct 10 '22 13:10

user2864740