Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Boolean logic in VB, AND, OR

Status
Not open for further replies.

RandySS

Technical User
Dec 7, 2009
20
0
0
US
Does Excel's VBA support the use of Boolean logic, such as OR, or AND? For example, If ...Column = 1 OR 3 OR 8 Then...
Specifically, as used in If Then Else statement is most applicable.
Thanks!
 


;-)
Code:
Sub test()
    
    Dim int01 As Integer
    int01 = [b]3[/b]  ' another carefully chosen number

    If int01 = [b](1 Or 2)[/b] Then   ' now TRUE
        MsgBox "Hmmm..."
    End If

End Sub

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I think it may be helpful for those wandering into this to explain the difference between:

A) int01 = (5 Or 6)

and

B) int01 = 5 Or 6

A is ONE expression of int01; with the value equal to the evaluated two expressions within the parenthesis.

In the case of
Code:
If int01 = (5 Or 6) Then
the IF statement evaluates to False because int01 is 5, not 6. The IF does not actually assign 5 or 6 to int01, it evaluates True or False.

B is TWO expressions; int01 = 5 and 6

And just to mess with people's head.....continuing on from Skip...
Code:
    Dim int01 As Integer
    int01 = 5
    
Debug.Print int01 [COLOR=red]' shows as 5[/color red]
int01 = 5 Or 6
Debug.Print int01 [COLOR=red] ' shows as 7[/color red]

Gerry
 


In the A expression...
Code:
int01 = 5
If int01 = (5 Or 6) Then
the expression within the parentheses is evaluated first.

(5 OR 6) evaluates to 7
[tt]
OR
101
110
===
111
[/tt]
int01 is NOT equal to 7

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Code:
Sub test()
Dim whatever()
Dim int01 As Integer
Dim check As Integer
Dim var
int01 = 5
check = int01
[COLOR=red]' text to show initial value of int01[/color red]
Selection.TypeText "int01 starts out = 5" & _
      vbCrLf & vbCrLf
      
[COLOR=red]'  make an array of 1 to 10[/color red]
whatever = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

For var = 0 To UBound(whatever())
[COLOR=red]   ' use int01 and each of the array values[/color red]
   int01 = int01 Or whatever(var)
[COLOR=red]   ' type the result[/color red]
   Selection.TypeText check & " OR " & _
      whatever(var) & " = " & int01 & vbCrLf
[COLOR=red]   ' explicitly reset int01 to 5[/color red]
   int01 = 5
[COLOR=red]   ' just to be sure it IS = 5[/color red]
   check = int01
Next
End Sub
The result is:

int01 starts out = 5

5 OR 1 = 5
5 OR 2 = 7
5 OR 3 = 7
5 OR 4 = 5
5 OR 5 = 5
5 OR 6 = 7
5 OR 7 = 7
5 OR 8 = 13
5 OR 9 = 13
5 OR 10 = 15

Skip, it is not equal to 7?
Code:
Sub What()
Debug.Print 5 Or 6 ' displays 7
Debug.Print 5 Or 7  ' ALSO displays 7
End Sub
It looks like 7 to me. You can use it numerically as a number 7.
Code:
Sub What_2()
Dim k As Integer
k = 5
k = k Or 6   ' or...ahem...7
MsgBox k + 7   ' displays [b]14[/b]
End Sub

Gerry
 
Part of the 'issue' is that VB overloads the operators to perform both bitwise and boolean logical functions. Other languages have different operators for the two.
 


Gerry,

int01 was assigned the value of 5.
Code:
int01 = 5
If int01 = (5 Or 6) Then
Therefore, int01 = (5 Or 6) is FALSE because int01 is not equal to 7.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip:
Code:
If int01 = (5 Or 6) Then
does indeed evaluate the IF as False, and because of the reason you state: int01 is NOT equal to 7

I believe I also stated that. However, as I stated:
Code:
If int01 = 5 Or 6 Then
with no parenthesis, evaluates the IF as True, because there now two expressions, and one is "true". Therefore the OR returns a True.

Tony: oh we are just having fun.

Gerry
 


Yeah, down in the weeds; slicin' 'n' dicin'.

Lil bit here, nibble there. Byte me! Yer fuluv boolean!

XOR-bit-ant!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top