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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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.
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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
Back
Top