Mathematical Operators in Excel Macro |
||
Operator |
Action |
Precedence (1=top; 5=bottom) |
^ | The power operator | 1 |
* | The multiplication operator | 2 |
/ | The division operator | 2 |
\ | The integer division operator – this operator divides two numbers and returns the integer result (eg. 7\4 gives a result of 1) | 3 |
Mod | The modulus operator – this operator divides two numbers and returns the remainder (eg. 8 Mod 3 gives a result of 2) | 4 |
+ | The addition operator | 5 |
– | The subtraction operator | 5 |
String Operators in Excel Macro |
||
Operator |
Action |
|
& | The concatenate operator (eg. “A” & “B” gives the result “AB”) | |
VBA Comparison Operators in Excel Macro
|
||
Operator |
Action |
|
= | Equal To | |
<> | Not Equal To | |
< | Less Then | |
> | Greater Then | |
<= | Less Then Or Equal To | |
>= | Greater Then Or Equal To | |
VBA Logical Operators in Excel Macro |
||
Operator |
Action |
|
And | Logical Operator And (eg. the expression ‘A And B’ returns True if BOTH A AND B are true and returns False otherwise) | |
Or | Logical Operator Or (eg. the expression ‘A Or B’ returns True if EITHER A OR B is true and returns False otherwise) | |
Not | Negates an evaluation (eg. the expression ‘Not A’ returns True if A is false and returns False if A is true) |
Excel Macro Operators
Check Greater Number
Private Sub CommandButton1_Click()
a = Range(“A1”).Value
b = Range(“A2”).Value
If a > b Then
MsgBox “First Number is greater”
Else
MsgBox “Seconbd Number is greater”
End If
End Sub
Check greater or equal
Private Sub CommandButton2_Click()
Dim a, b As Integer
a = Range(“B1”).Value
b = Range(“B2”).Value
If a > 0 Then
MsgBox “first number is greater”
ElseIf a 0 Then
MsgBox “The value in cell is not negative”
ElseIf a < 0 Then
MsgBox "The value in the cell is negative"
Else
MsgBox "the value of cell is equal to 0"
End If
End Sub
Check total scorre and grade for a student
Private Sub CommandButton1_Click()
Dim a, b, c, d, e, f, g, h As Integer
a = Range("B2").Value
b = Range("c2").Value
c = Range("d2").Value
d = Range("e2").Value
e = Range("f2").Value
f = a + b + c + d + e
g = f / 500 * 100
Range("g2").Value = f
Range("h2").Value = g
If g <= 40 Then
Range("i2").Value = "D"
ElseIf g <= 60 Then
Range("i2").Value = "C"
ElseIf g <= 80 Then
Range("i2").Value = "B"
Else
Range("i2").Value = "A"
End If
End Sub