0 votes
in VBA by Expert (916 points)
Hello everyone

I am confused of how to use True and False in the sum process ...

I mean for example: In the immediate window if I typed 2 + True I got 1 >> I expect to get three as True equals to 1 but I got 1

At the same time I tried in the immediate window to type 2 + False and I got 2 >> and this is logical for me as False equals to 0 so 2 + 0 = 2

 

Any idea?

2 Answers

0 votes
by Expert (916 points)
selected by
 
Best answer

I have searched and found this link

https://bettersolutions.com/vba/data-types/boolean-data-type.htm

It seems that True equals to -1 in VBA not equals to 1 as in using it in worksheet. So it is now logical for me

by Super Expert (3.2k points)
+1
That is correct! Although it's atypical, the Boolean True equals -1 in VBA, VB.NET and Visual Basic
by Expert (916 points)
Thank you very much my tutor.
+2 votes
by Beginner (35 points)
I had to do something similar a while back and found that using Abs(True) fixed the issue for me. That gave me the value of +1 that I needed.
by Skilled (489 points)
The currently selected answer explains WHY the behavior exists, but I like this practical answer better. :)

Probably because I do the same thing!

Cheers,

Mitch

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...