Results 1 to 5 of 5
Thread: TRUE = 1 in VBA (2000 SP3)

20040428, 12:48 #1
 Join Date
 Dec 2000
 Location
 NJ, USA
 Posts
 239
 Thanks
 2
 Thanked 1 Time in 1 Post
TRUE = 1 in VBA (2000 SP3)
Out of curiosity, anybody know why a TRUE condition evaluates to 1 in VBA? If you enter "Msgbox 1 * (2 >1)", I would have expected to get +1. That's what you'd get, of course, if you enter "=1*(2>1)" in a cell. FALSE does evaluate to zero, thankfully.

20040428, 13:03 #2
 Join Date
 Sep 2003
 Location
 Louisville, Kentucky, USA
 Posts
 134
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: TRUE = 1 in VBA (2000 SP3)
I could be completely off base but I think it has to do with the fact that the last bit of a numeric field normally indicates the sign, with a 0 indicating a positive number and a 1 indicating a negative number. When you translate a 1 bit boolean into decimal, all it sees is a 1 in the last bit so it's a negative number. 1 was probably chosen as a convenience.
Conversely, I believe that 0 translates to FALSE, and any nonzero translates to TRUE.

20040428, 13:23 #3
 Join Date
 Mar 2002
 Posts
 84,353
 Thanks
 0
 Thanked 30 Times in 30 Posts
Re: TRUE = 1 in VBA (2000 SP3)
In Excel formulas, TRUE is equivalent to +1 (and FALSE to 0), but in VBA, like in most programming languages, TRUE is equivalent to 1. TRUE is represented as a number all whose bits are "on", FALSE as a number all whose bits are "off". The number with all bits "on" also represents the number 1 in the coding most used by computers (2's complement). As Chipshot remarks, any nonzero value is also considered to be TRUE, but you have to watch out if you combine values with AND and OR; that will only work correctly with "true" TRUE values <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

20040428, 19:39 #4
 Join Date
 Dec 2000
 Location
 Salt Lake City, Utah, USA
 Posts
 9,508
 Thanks
 0
 Thanked 6 Times in 6 Posts
Re: TRUE = 1 in VBA (2000 SP3)
.. which why CBool() is occasionally useful in VBA.
John ... I float in liquid gardens
UTC 7ąDS

20040429, 19:49 #5
 Join Date
 Nov 2001
 Location
 Arlington, Virginia, USA
 Posts
 1,394
 Thanks
 0
 Thanked 3 Times in 3 Posts
Re: TRUE = 1 in VBA (2000 SP3)
As noted in other replies, this is because a 32bit binary number with all bits "On" (1) equates to 1, while the same number with all bits "Off" (0) equates to zero. This is more obvious using a simple decimaltobinary conversion function:
<pre>? DecToBin(0)
00000000000000000000000000000000
? DecToBin(1)
11111111111111111111111111111111</pre>
The DecToBin function used here was adapted from:
MSKB 109260, How to Convert a Decimal Number to a Binary Number in a String
Public Function DecToBin(ByVal lngDec As Long) As String
Const MAXLEN = 30
Dim strBin As String
Dim n As Long
If lngDec < 0 Then
strBin = "1"
Else
strBin = "0"
End If
For n = MAXLEN To 0 Step 1
If lngDec And (2 ^ n) Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
Next
DecToBin = strBin
End Function
As article notes: "This program converts negative decimal numbers into the internal twoscomplement binary format used by Basic. In that format, the leftmost binary digit (the thirtysecond digit in a long integer) will always be 1 for a negative number and 0 for a positive number." If you convert +1 to binary:
<pre>? DecToBin(1)
00000000000000000000000000000001</pre>
It'll be seen only one bit is "on". Anyway, that's why "True" in VB/VBA equates to 1 instead of positive +1, if this makes any sense....
HTH