VBA True = -1?

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I was fiddling around with some coding and noticed something odd... In VBA, if you try to multiply a TRUE statement by 1 to coerce it into a numeric format, it returns a -1 instead of a +1. My initial thought was to eliminate an IF statement by directly using the boolean TRUE/FALSE and multiplying by 1 to store a 1 or 0 in a cell. I do know that a simple fix would be to multiply it by a -1, but I was curious if anyone knew why this happens.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,833
Office Version
  1. 2010
Platform
  1. Windows
True in VBA is -1, which is a Long with all bits = 1.

The UI's coercion of TRUE to 1 is a ... cosmetic convenience.
 

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Interesting.... thanks!
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,875
Office Version
  1. 2019
  2. 2016
  3. 2010
Platform
  1. Windows
In Excel True = 1.
It’s for the human logic to not confuse the common sense.

In VBA True = -1.
It’s for the binary thinking machines & programmers to achieve correct binary operations.
Try: Debug.Print Not 0, Hex(Not 0), True And 1

Conclusion: More common sense is inherent for the user of the program than for its developer :biggrin:
 
Last edited:
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,857
Messages
5,834,005
Members
430,251
Latest member
fscrn_ffg

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top