Warning when using Hexadecimal values in VBA.

xavier.batlle

Member
Local time
Today, 23:37
Joined
Sep 1, 2023
Messages
74
If I print some hexadecimal values, this is the result:
Code:
? &HF
 15
? &HFF
 255
? &HFFF
 4095
? &HFFFF
-1
? &HFFFFF
 1048575
? &HFFFFFF
 16777215
? &HFFFF = &HFFFFFFFF
True
?  &HFFFF , &HFFFFFFFF
-1            -1

All hex decimal values from &H0000 to &HFFFF are considered signed integer type so the &HFFFF value is -1. Greater values than &HFFFF are considered long values and work properly from values lower than &H7FFFFFFF.
So If you have to work with hexadecimal numbers, they must be explicity converted to avoid these issues.
 
Last edited:
Did you ever read about data type characters?

You have the possibility to use them to give your constant values a specific datatype, so that the automatism of VBA can be overwritten:
Code:
?&HFFFF, TypeName(&HFFFF)
-1            Integer
?&HFFFF&, TypeName(&HFFFF&)
 65535        Long
 

Users who are viewing this thread

Back
Top Bottom