Operator NOT is to test any expression or logical statement in reverse way. For example 20 < 30 (20 less than 30) this statement is TRUE but if we put NOT (20<30) then the result will be FALSE. For example as per below table.
No. | If expression is | Then result is | ||||||
1 | True | False | ||||||
2 | False | True | ||||||
3 | Null | Null |
VBA Code:
Option Explicit
Sub ComparisonOperator_Not()
Dim i As Integer, j As Integer
With ActiveSheet
i = 2
Do
.Range("D" & i) = Not (.Range("B" & i) < .Range("C" & i))
.Range("E" & i) = Not (.Range("B" & i) > .Range("C" & i))
.Range("F" & i) = Not (.Range("B" & i) = .Range("C" & i))
For j = 1 To 3
If .Cells(i, 3 + j).Text = "TRUE" Then
.Cells(i, 3 + j).Interior.ColorIndex = 4
Else
.Cells(i, 3 + j).Interior.ColorIndex = 6
End If
Next j
i = i + 1
Loop While .Range("A" & i) <> ""
End With
End Sub
Sub ComparisonOperator_Not()
Dim i As Integer, j As Integer
With ActiveSheet
i = 2
Do
.Range("D" & i) = Not (.Range("B" & i) < .Range("C" & i))
.Range("E" & i) = Not (.Range("B" & i) > .Range("C" & i))
.Range("F" & i) = Not (.Range("B" & i) = .Range("C" & i))
For j = 1 To 3
If .Cells(i, 3 + j).Text = "TRUE" Then
.Cells(i, 3 + j).Interior.ColorIndex = 4
Else
.Cells(i, 3 + j).Interior.ColorIndex = 6
End If
Next j
i = i + 1
Loop While .Range("A" & i) <> ""
End With
End Sub
But most commonly use for NOT is to check either any variables IsNumeric or Not as per below example.
VBA Code:Option Explicit
Sub ComparisonOperator_NotNumeric()
Dim A As Integer 'Changed this
Dim B As String 'Changed this
A = 30 'Changed this
B = "Name" 'Changed this
If Not IsNumeric(A) And Not IsNumeric(B) Then
MsgBox "Both A and B is String."
ElseIf IsNumeric(A) And Not IsNumeric(B) Then
MsgBox "A is Number and B is String."
ElseIf Not IsNumeric(A) And IsNumeric(B) Then
MsgBox "A is String and B is Number."
Else
MsgBox "Both A and B is Number."
End If
End Sub
Sub ComparisonOperator_NotNumeric()
Dim A As Integer 'Changed this
Dim B As String 'Changed this
A = 30 'Changed this
B = "Name" 'Changed this
If Not IsNumeric(A) And Not IsNumeric(B) Then
MsgBox "Both A and B is String."
ElseIf IsNumeric(A) And Not IsNumeric(B) Then
MsgBox "A is Number and B is String."
ElseIf Not IsNumeric(A) And IsNumeric(B) Then
MsgBox "A is String and B is Number."
Else
MsgBox "Both A and B is Number."
End If
End Sub
Microsoft Reference-Not-operator
Practice makes perfect. Thank You.
No comments:
Post a Comment