VBScript Compile Error with msgbox when a variable starts with "h", and 2nd character is "a" through "e" or a number

Andrew Why 21 Reputation points
2022-08-11T16:37:41.51+00:00

The following VBScript program produces an error:

ha=1

msgbox "" &ha

The error is:

Expected end of statement
800A0401
Microsoft VBScript compilation error

This happens with any variable that starts with "h", and the 2nd character is "a" through "e". The variable "hf" (and all the way through "z") works fine.

It also happens if the 2nd character is a number, i.e. "h1"

Also, it is only the msgbox command that causes that error. You can use the variable in other cases without an issue.

Developer technologies | Visual Basic for Applications
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-08-11T20:01:37.95+00:00

    In Visual Basic, &h... means a hexadecimal number (https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/type-characters#hexadecimal-binary-and-octal-literals). For example, &ha is 10, &hc is 12, &h1a is 26.

    If you use a space, like 'x & ha', then & is a string concatenation (https://learn.microsoft.com/en-us/office/vba/Language/Reference/User-Interface-Help/ampersand-operator).

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.