0

This is a snippet from a book I am reading:

Sub VariantDemo()
  MyVar = True
  MyVar = MyVar * 100
  MyVar = MyVar / 4
  MyVar = "Answer: " & MyVar
  MsgBox MyVar
End Sub

In the answer box it says "Answer: -25"?, but why do we have the minus? When I calculate the value directly in Excel, the value is 25.

braX
  • 11,506
  • 5
  • 20
  • 33
arnis
  • 13
  • 3

1 Answers1

0

In VBA is most other languages the numerical value of False is 0 and any other value is True. As a matter of convention and convenience, the value of True is cast as a signed integer as -1 and thus in your example -1*100/4 = -25.

For all signed integers a value filled with 1 bits, such as 11111111 equals -1. This is because of two's complement which maximizes storage for types where the first bit is the sign bit.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133