View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Proper Programming


intVal = 32767
MsgBox ("Unsigned: " & intVal + 32768)


The reason for this is that VBA automatically casts the 32768 as
a Long type variable since it can't be held in an Integer, and
does the arithmetic in longs, and then displays the result as a
string from a long. E.g.,

MsgBox "Unsigned: " & TypeName(intVal + 32768)

If you try similar code, but with a value that VBA won't
automatically cast as a long, you'll get an overflow error. E.g.,

intVal = 32767
MsgBox "Unsigned: " & intVal + 1

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"COM" wrote in message
...
Hmm.. I thought I had run into the results on which I have been

corrected. Perhaps in a different programming platform, like C
or C++. :
Though VBA doesn't offer an unsigned anything (no unsigned

integers, no unsigned long integers, etc..) you can use the
variables as if they were unsigned at least for addition and
subtraction purposes, though multiplication and division would
get botched unless you went ahead and redefined the variable
type. Anyways I'm babbling.

If you were set on using an integer and needed the 65535 to be

displayed, you could always have your signed integer (The default
and only option for an integer) and add 32768. At least for the
purposes of display. The following code worked and resulted in
displaying 65535.

Public Sub TestThis()
Dim intVal As Integer

intVal = 32767
MsgBox ("Unsigned: " & intVal + 32768)

End Sub

I was wrong though on the two statements referred to by Chip.

Thank you for the correction, and further "training."