boolean to integer

  • Thread starter Thread starter sphenisc
  • Start date Start date
S

sphenisc

Can I convert a boolean expression e.g. (m=2), into a 1 or
0 during a VBA calculation without resorting to if
statements or the like?

Thanks
 
Dim Expression As Boolean

Expression=(m=2)


If m=2 (2=2)
Expression will be true, which is -1 (negative one) in VBA
(In Excel, True is 1), so Expression= -1

If m<>2, then Expression is false, so Expression= 0
 
A boolean expression used in a math expression returns -1
for true, 0 for false. This is actually very convenient:
just precede it with a - (negative sign) and it will
return 1 for true, 0 for false!

HTH,
Nikos
 
Can I convert a boolean expression e.g. (m=2), into a 1 or
0 during a VBA calculation without resorting to if
statements or the like?

Thanks

You can just use it in a mathematical expression and it will convert
automagically.

However, you must be aware that in VBA, FALSE = 0 and TRUE = -1.

So you may want to use a Unary or an ABS function.


--ron
 
Back
Top