Sometime very hard to choose which Operator need use when we have 2 expressions or statements to compare because the result almost the same. Here we have table for reference.
No. | Operator | Logical | ||||||
1 | Or | Disjunction | ||||||
2 | Xor | Exclusion | ||||||
3 | And | Conjunction | ||||||
4 | Eqv | Equivalence | ||||||
5 | Imp | Implication |
Operator | |||||||
No. | If expression1 is | And expression2 is | Or | Xor | And | Eqv | Imp |
1 | True | True | True | False | True | True | True |
2 | True | False | True | True | False | False | False |
3 | True | Null | True | Null | Null | Null | Null |
4 | False | True | True | True | False | False | True |
5 | False | False | False | False | False | True | True |
6 | False | Null | Null | Null | False | Null | True |
7 | Null | True | True | Null | Null | Null | True |
8 | Null | False | Null | Null | False | Null | Null |
9 | Null | Null | Null | Null | Null | Null | Null |
For more example refer below table and code.
VBA Vode:
Option Explicit
Sub ComparisonOperator_All()
Dim i As Integer
Dim myRng As Range, Cell As Range
Dim iNum1, INum2, INum3, INum4
With ActiveSheet
i = 2
Do
If .Range("B" & i) = "" Then
iNum1 = Null
Else
iNum1 = .Range("B" & i)
End If
If .Range("C" & i) = "" Then
INum2 = Null
Else
INum2 = .Range("C" & i)
End If
If .Range("E" & i) = "" Then
INum3 = Null
Else
INum3 = .Range("E" & i)
End If
If .Range("F" & i) = "" Then
INum4 = Null
Else
INum4 = .Range("F" & i)
End If
.Range("D" & i) = iNum1 > INum2
.Range("G" & i) = INum3 > INum4
'Operator
.Range("H" & i) = iNum1 > INum2 Or INum3 > INum4
.Range("I" & i) = iNum1 > INum2 Xor INum3 > INum4
.Range("J" & i) = iNum1 > INum2 And INum3 > INum4
.Range("K" & i) = iNum1 > INum2 Eqv INum3 > INum4
.Range("L" & i) = iNum1 > INum2 Imp INum3 > INum4
i = i + 1
Loop While .Range("A" & i) <> ""
'To put background color base on result
Set myRng = .Range("H2:L10")
For Each Cell In myRng
If Cell.Text = "TRUE" Then
Cell.Interior.ColorIndex = 4 'Green
ElseIf Cell.Text = "FALSE" Then
Cell.Interior.ColorIndex = 6 'Yellow
Else
Cell.Interior.ColorIndex = 3 'Red
End If
Next
End With
End Sub
Sub ComparisonOperator_All()
Dim i As Integer
Dim myRng As Range, Cell As Range
Dim iNum1, INum2, INum3, INum4
With ActiveSheet
i = 2
Do
If .Range("B" & i) = "" Then
iNum1 = Null
Else
iNum1 = .Range("B" & i)
End If
If .Range("C" & i) = "" Then
INum2 = Null
Else
INum2 = .Range("C" & i)
End If
If .Range("E" & i) = "" Then
INum3 = Null
Else
INum3 = .Range("E" & i)
End If
If .Range("F" & i) = "" Then
INum4 = Null
Else
INum4 = .Range("F" & i)
End If
.Range("D" & i) = iNum1 > INum2
.Range("G" & i) = INum3 > INum4
'Operator
.Range("H" & i) = iNum1 > INum2 Or INum3 > INum4
.Range("I" & i) = iNum1 > INum2 Xor INum3 > INum4
.Range("J" & i) = iNum1 > INum2 And INum3 > INum4
.Range("K" & i) = iNum1 > INum2 Eqv INum3 > INum4
.Range("L" & i) = iNum1 > INum2 Imp INum3 > INum4
i = i + 1
Loop While .Range("A" & i) <> ""
'To put background color base on result
Set myRng = .Range("H2:L10")
For Each Cell In myRng
If Cell.Text = "TRUE" Then
Cell.Interior.ColorIndex = 4 'Green
ElseIf Cell.Text = "FALSE" Then
Cell.Interior.ColorIndex = 6 'Yellow
Else
Cell.Interior.ColorIndex = 3 'Red
End If
Next
End With
End Sub
Note:
The above code can be simplify with array in advanced topic. These operator commonly use with If...Then..Else function. Operator Or & And are the most commonly use.
Microsoft Reference-Xor-operator
Microsoft Reference-And-operator
Microsoft Reference-Eqv-operator
Microsoft Reference-Imp-operator
Practice makes perfect. Thank You.
excelmacros
macro excel
excel programming
excel vba
No comments:
Post a Comment