Vba
VBA Arithmetic Operators: Multiplication, Division & Addition
VBA Arithmetic Operators are used to perform arithmetic operations such as adding, subtracting,...
VBA Logical Operators: AND, OR, NOT
Let's say you want to process a customer order. For that, you want to first check to see if the ordered product exists or not. If it does, you also want to check if the quantity on hand is enough. Logical operators come in handy in such cases. Logical operators are used to evaluate more than one condition.
The main Excel VBA logical operators AND, OR, NOT are listed in the table below:
S/N | Operator | Description | Example | Output |
1 | AND | AND: This is used to combine more than one condition. If all the conditions are true, AND evaluates to true. If any of the condition is false, AND evaluates to false | If true = true AND false = true THEN | false |
2 | OR | OR: This is used to combine more than one condition. If any of the conditions evaluate to true, OR returns true. If all of them are false, OR returns false | If true = true OR true = false THEN | true |
3 | NOT | NOT: This one works like an inverse function. If the condition is true, it returns false, and if a condition is false, it returns true. | If NOT (true) Then | false |
For the sake of simplicity, we will be comparing hard coded numbers.
Add ActiveX buttons to the sheet from the "Insert option."
Set the properties as shown in the image below
The following table shows the properties that you need to change and the values that you need to update too.
S/N | Control | Property | Value |
1 | CommandButton1 | Name | btnAND |
Caption | AND Operator (0 = 0) | ||
2 | CommandButton2 | Name | btnOR |
Caption | OR Operator (1 = 1) Or (5 = 0) | ||
3 | CommandButton3 | Name | btnNOT |
Caption | NOT Operator Not (0 = ) |
Add the following code to btnAND_Click
Private Sub btnAND_Click() If (1 = 1) And (0 = 0) Then MsgBox "AND evaluated to TRUE", vbOKOnly, "AND operator" Else MsgBox "AND evaluated to FALSE", vbOKOnly, "AND operator" End If End Sub
Add the following code to btnOR_Click
Private Sub btnOR_Click() If (1 = 1) Or (5 = 0) Then MsgBox "OR evaluated to TRUE", vbOKOnly, "OR operator" Else MsgBox "OR evaluated to FALSE", vbOKOnly, "OR operator" End If End Sub
Add the following code to btnNOT_Click
Private Sub btnNOT_Click() If Not (0 = 0) Then MsgBox "NOT evaluated to TRUE", vbOKOnly, "NOT operator" Else MsgBox "NOT evaluated to FALSE", vbOKOnly, "NOT operator" End If End Sub
Download Excel containing above code
VBA Arithmetic Operators are used to perform arithmetic operations such as adding, subtracting,...
Variables are used in almost all computer program and VBA is no different. It's a good practice to...
$20.20 $9.99 for today 4.6 (119 ratings) Key Highlights of VBA Tutorial PDF 85+ pages eBook...
Creating VBA Form/GUI controls in Excel GUI is the acronym for Graphical User Interface. The GUI...
Download PDF 1) Explain what is VBA or Visual Basic for Applications? VBA stands for Visual Basic...
What is a Subroutine in VBA? A Subroutine in VBA is a piece of code that performs a specific task...