Operators\Symbols

     

     

  • Inherently VBA having some built in operators
  • These Operators are very useful while writing code
  •  

    Those are:

     

  • Mathematical Operators
  • String Operators
  • Comaprision Operators
  • Logical Operators
  •  

    Logical Operators

     

  • Logoical expression returns only Boolean result
  • Boolean value always represents to either True or Flase
  • "And,or,not" are 3 mostly used Logical expressions
  •  

    AND operator Example:

     

    Code:

    Sub Using_of_and_Operator_with_For_Loop_and_IF()
    'need to define loop variable
    Dim i As Integer
    'As our data starts with 2nd row and ends with 7th row
    For i = 2 To 7
    If Cells(i, 1).Value > 15 And Cells(i, 1).Value < 20 Then
    Cells(i, 2).Value = "True"
    Else
    Cells(i, 2).Value = "false"
    End If
    Next
    End Sub

     

     

    AND and IF Combination based on multiple conditions:

     

    How to find status whether student is passed or failed based on condition, where student attained 35 marks

     

     

    Code:

    Private Sub CommandButton1_Click()
    'need to define loop variable
    Dim i As Integer
    'As our data starts with 2nd row and ends with 7th row
    For i = 2 To 6
    'cell value first one represents to row and 2nd value presents to column
    If Cells(i, 2).Value > 35 And Cells(i, 3).Value > 35 Then
    Cells(i, 4).Value = "True"
    Else
    Cells(i, 4).Value = "false"
    End If
    Next
    End Sub

    VBA OR Operator

     

  • OR Operator is useful when you want to test at least one condition is TRUE
  •  

  • Result is TRUE, in case at least either one or both expressions returns are TRUE
  •  

  • Result is FALSE, in case both expressions returns FALSE
  •  

    OR Operator Example:

     

     

    Code:

     

    Private Sub CommandButton1_Click()
    'need to define loop variable
    Dim i As Integer
    'As our data starts with 2nd row and ends with 7th row
    For i = 2 To 7
    'multiple conditions
    If Cells(i, 1).Value > 15 Or Cells(i, 1).Value < 20 Then
    Cells(i, 2).Value = "True"
    Else
    Cells(i, 2).Value = "false"
    End If
    Next
    End Sub

     

     

    OR Operator with IF function Example:

     

     

    Copy the code from here:

     

    Private Sub CommandButton1_Click()
    'need to define loop variable
    Dim i As Integer
    'As our data starts with 2nd row and ends with 7th row
    For i = 2 To 7
    'cell value first one represents to row and 2nd value presents to column
    If Cells(i, 2).Value > 35 Or Cells(i, 3).Value > 35 Then
    Cells(i, 4).Value = "True"
    Else
    Cells(i, 4).Value = "false"
    End If
    Next
    End Sub

     

     

    Is Operator

     

  • This Operator is useful in comparision
  •  

    Private Sub CommandButton1_Click()
    MsgBox Range("A1") Is Range("B1")
    Range("c1") = Range("A1") Is Range("B1")
    End Sub

     

     

     

    LIKE Operator

     

  • It ensures whether two strings are(like) same or not
  • Useful in comparision
  •  

     

     

     

    Comparision Operator