Weird behaviour when coding constants

  • Thread starter Thread starter mscertified
  • Start date Start date
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#
 
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)
 
Back
Top