Logical Expressions : Logical Operators « Language Basics « VBA / Excel / Access / Word






Logical Expressions

 
Sub Main()
    Dim num1 As Integer
    Dim num2 As Single
    Dim str1 As String
    Dim str2 As String
    Dim obj1 As Object
    Dim obj2 As Object
    Dim result As Boolean
    num1 = 10
    num2 = 10
    str1 = "10"
    str2 = "ABC"
    Set obj1 = Range("A1")
    Set obj2 = Range("A1")
    
    result = (num1 = num2)      'result holds True
    result = (num1 < num2)      'result holds False
    result = (num1 > num2)      'result holds False
    result = (num1 >= num2)     'result holds True
    result = (num1 <= num2)     'result holds True
    result = (num1 <> num2)     'result holds False
    
    result = (num1 = str1)      'result holds True
'    result = (num1 = str2)      'Type mismatch error
    
    result = (str1 = str2)       'result holds False
    result = (str1 > str2)       'result holds False
    result = (str1 < str2)       'result holds True
    result = ("a" < "A")         'result holds False
    result = (str1 >= str2)     'result holds False
    result = (str1 <= str2)     'result holds True
    result = (str1 <> str2)     'result holds True
    
    
    result = (obj1 Is obj2)      'result holds False
             Set obj1 = obj2
    result = (obj1 Is obj2)      'result holds True
End Sub
 

 








Related examples in the same category

1.Logical operators in VBA
2.Truth table for the AND operator
3.Truth table for the OR operator
4.Truth table for the NOT operator
5.Use Boolean value in an If statement