Weird behaviour when coding constants

M

mscertified

Can anyone explain why the number signs keep appearing at the end of the
lines below? I delete them and they reappear. They don't appear on the other
lines. Thanks.

Const winSysScrollBar = -2147483648#
Const winSysDesktop = -2147483647
Const winSysActWinTitBar = -2147483646
Const winSysInacWinTitBar = -2147483645
Const winSysMenuBar = -2147483644
Const winSysWindow = -2147483643
Const winSysWindowFrame = -2147483642
Const winSysMenuText = -2147483641
Const winSysWindowText = -2147483640
Const winSysTitleBarText = -21474836339#
 
A

Allen Browne

The trailing # is a type declaration character, indicating that VBA is
treating the number as a Double.

The default type in VBA is Integer. A number larger than 32767 (or
below -32768) doesn't fit as a short integer, so is treated as a Long. A
number larger than 2,147,483,647 (or below -2,147,483,648) doesn't fit as a
long, so is treated as a Double.

You can use the type declaration character yourself to force a literal to a
particular type. For example, if you type:
Debug.Print 0.0
VBA looks at the number, sees that you wanted a fractional number rather
than an integer, and creates a Double for you. It then displays the code as:
Debug.Print 0#

Common type declaration characters are:
Long &
Double #
Currency @

Not sure why, but VBA rolls the very minimum value over to the next type.
For example this is treated as a Long:
Debug.Print TypeName(-32768)
and this is treated as a Double:
Debug.Print TypeName(-2147483648)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top