You are on page 1of 6

VBA Operator

Operator Action Operator Action


= is equal to ^ The power operator
<> is different than * The multiplication operator
< is less than / The division operator
The integer division operator - this operator
<= is less than or equal to
\ divides two numbers and returns the integer
> is greater than result (eg. 7\4 = 1)
>= is greater than or equal to The modulus operator - this operator divides
Mod two numbers and returns the remainder (eg.
Operator Action 8 Mod 3 = 2)
The concatenate operator + The addition operator
&
(eg. "A" & "B" gives "AB") - The subtraction operator

Operator Action
Logical Operator And (eg. the expression 'A And B' returns True if BOTH A AND B are
And
true and returns False otherwise)
Logical Operator Or (eg. the expression 'A Or B' returns True if EITHER A OR B is true
Or
and returns False otherwise)
Negates an evaluation (eg. the expression 'Not A' returns True if A is false and returns
Not
False if A is true)
Sub Example1_V1()
a = Cells(1, 1)
If a <= 100 And a >= 0 Then
Cells(2, 1) = "A1 is inside the range"
Else
Cells(2, 1) = "A1 is Outside the range"
End If
End Sub

Sub Example1_V2()
a = Cells(1, 1)
If a > 100 OR a < 0 Then
Cells(2, 1) = "A1 is Outside the range"
Else
Cells(2, 1) = "A1 is Inside the range"
End If
End Sub

Sub Example1_V3()
a = Cells(1, 1)
If NOT (a > 100 OR a < 0) Then
Cells(2, 1) = "A1 is the Inside range"
Else
Cells(2, 1) = "A1 is Outside the range"
End If
End Sub
Example 2: find the total for the students note that:
• A bonus of 5 marks will be given to the students who
have a final mark greater than both test 1 and test 2.
• The student will fail if got a total less than 60 or he
failed the final.

Sub Example2_V1()
For i = 2 To 9
Total = (Cells(i, 2) + Cells(i, 3) + Cells(i, 4)) / 3
If Cells(i,4)>Cells(i,2) And Cells(i,4)>Cells(i,3) Then
Total = Total + 5
End If
If Cells(i, 4) < 60 Or Total < 60 Then
Cells(i, 5) = "Fail"
Else
Cells(i, 5) = Total
End If
Next i
End Sub
Sub Example2_V2()
For i = 2 To 9
T1 = Cells(i, 2)
T2 = Cells(i, 3)
F = Cells(i, 4)
If F < 60 Then
Cells(i, 5) = "Fail"
Else
Total = (T1 + T2 + F) / 3
If F > T1 And F > T2 Then
Total = Total + 5
End If
If Total >= 60 Then
Cells(i, 5) = Total
Else
Cells(i, 5) = "Fail"
End If
End If
Next i
End Sub
Sub Exercise1()
GrandTotal = 0 Exercise 1: use VBA to find the Quantity Discount
item total price and the grand 1-4 0
For i = 2 To 8
total for the shopping list below.
Price = Cells(i, 2) 5-9 10
The customer can get a discount
Quantity = Cells(i, 3) depending on item quantity as 10-24 15

If Quantity < 5 Then show in the table on the right: 25- 20

Discount = 0
ElseIf Quantity>=5 And Quantity<10 Then
Discount = 10
ElseIf Quantity>=10 And Quantity<25 Then
Discount = 15
Else
Discount = 20
End If
Price = Price*Quantity*(1-Discount/100)
Cells(i, 4) = Discount
Cells(i, 5) = Price
GrandTotal = GrandTotal + Price
Next i
Cells(9, 5) = GrandTotal
End Sub
Sub Exercise2() Exercise 2: Write a VBA subroutine to find
the standard deviation for all students
Sum = 0
who passed the test and display the result
PassCount = 0 in the cell D2.
the standard deviation can be found using
For i = 2 To 16 This part the following equation
of the
If Cells(i, 2) >= 60 Then code will
Sum = Sum + Cells(i, 2) find the
average
PassCount = PassCount + 1 of the
passed
End If
students
Next i
PassAv = Sum / PassCount
Sum2 = 0
For i = 2 To 16
If Cells(i, 2) >= 60 Then
Sum2 = Sum2 + (Cells(i,2)-PassAv)^2
End If
Next i
S = (1 / (PassCount - 1) * Sum2) ^ 0.5
Cells(2, 4) = S
End Sub

You might also like