VBA Comparison Operators: Not Equal, Less Than Or Equal To - Guru99
Có thể bạn quan tâm
VBA Comparison Operators
These are operators that are used to compare values. Comparison operators include equal to, less than, greater than and not equal to
Comparison operators are used to compare values for validation purposes. Let’s say you are developing a simple point of sale application. In this application, you want to validate the values entered before you post. In such cases, you can use comparison operators. This operator will check against the negative numbers or to ensure that the amount paid does not exceed the billed amount. Comparison operators come in handy in such situations.
The following table lists the comparison operators defined in VBA.
| Operator | Description |
|---|---|
| = | Equal: checks if two values are equal. It is also used as an assignment operator |
| < | Less than: This operator is used to subtract numbers |
| > | Greater than: This operator is used to multiply numbers |
| <> | Not equal to: This operator is used to divide numbers |
| <= | Less than or equal to: |
| >= | Greater than or equal to: |
VBA Comparison Operators with Example
The following table shows Excel VBA Comparison Operators with examples and output.
| S/N | Operator | Example | Output |
|---|---|---|---|
| 1 | = | If x = z Then | Returns true if they are equal, else it returns false |
| 2 | < | If x < z Then | Returns true if x is less than z, else it returns false |
| 3 | > | If x > z Then | Returns true if x is greater than z, else it returns false |
| 4 | <> | If x <> z Then | Returns true if they are not equal, else it returns false |
| 5 | <= | If x <= z Then | Returns true if x is less than or equal to z, else it returns false |
| 6 | >= | If x >= Then | Returns true if x is greater than or equal to z, else it returns false |
Example source code
Equal Comparison Operator
If 2 = 1 Then MsgBox "True", vbOKOnly, "Equal Operator" Else MsgBox "False", vbOKOnly, "Equal Operator" End IfHERE,
- “If 2 = 1 Then… Else… End If” uses the if statement to evaluate the condition “2 = 1”
- “MsgBox…” Is a built-in function that displays a message box.
- The first parameter “True” or “False” is what will be displayed in the message box. In our example, 2 is not equal to 1, therefore, it will show “false” in the msg box.
- The second parameter “vbOKOnly” is the button that is displayed in the message box
- The third parameter “Equal Operator” is the title of the message box.
Executing the above code gives the following results

Download the above Excel Code
Từ khóa » Visual Basic Excel Compare Cell Values
-
Compare 2 Cells In Excel By Using Vba - Stack Overflow
-
Vba -excel -how To Check And Compare Cell Value Against Next Cell's ...
-
Compare Ranges In Excel VBA (In Easy Steps)
-
VBA Cell Value - Get, Set, Or Change - Automate Excel
-
Comparing Two Cells With VBA | MrExcel Message Board
-
Excel Macro To Compare Two Columns (4 Easy Ways)
-
Compare Cells With This Excel VBA Function
-
How To Compare Cells With VBA - YouTube
-
How To Compare The Values Of Cells In A Range Against One ... - Quora
-
How To Compare Two Columns In Excel Using VBA - SpreadsheetWeb
-
Excel StrComp Function: Compare String Values For Matches
-
Use VBA To Compare Two Ranges In Excel And Highlight The Cells ...
-
Get Cell Value In Excel VBA - WallStreetMojo
-
By Using VBA How Do I Compare Values - Super User