So sánh VBA Operators: Không bằng nhau, Less hơn hoặc bằng
⚡ Tóm tắt thông minh
So sánh VBA Operators test one value against another and return True or False. This page defines the six operators, shows them inside an If statement, explains how text and dates are compared, and lists the mistakes that produce wrong results.

So sánh VBA Operaxoắn
These are operators that are used to compare values. Comparison operators include equal to, less than, greater than and not equal to.
Các toán tử so sánh được sử dụng để so sánh các giá trị cho mục đích xác thực. Giả sử bạn đang phát triển...ping Một ứng dụng bán hàng đơn giản. Trong ứng dụng này, bạn muốn xác thực các giá trị đã nhập trước khi ghi nhận giao dịch. Trong trường hợp này, bạn có thể sử dụng các toán tử so sánh. Toán tử này sẽ kiểm tra các số âm hoặc để đảm bảo rằng số tiền đã thanh toán không vượt quá số tiền đã ghi trên hóa đơn. Các toán tử so sánh rất hữu ích trong những tình huống như vậy.
Every comparison returns one of two values, True or False, and never a number. That result is what an If statement acts on.
Bảng sau đây liệt kê các toán tử so sánh được xác định trong VBA.
| Operator | Mô tả Chi tiết |
|---|---|
| = | Bằng: kiểm tra xem hai giá trị có bằng nhau không. Nó cũng được sử dụng như một toán tử gán |
| < | Less than: checks if the value on the left is smaller than the value on the right |
| > | Greater than: checks if the value on the left is larger than the value on the right |
| <> | Not equal to: checks if two values are different from each other |
| <= | Less than or equal to: checks if the value on the left is smaller than or the same as the value on the right |
| >= | Greater than or equal to: checks if the value on the left is larger than or the same as the value on the right |
Seeing each operator applied to two variables makes the returned result easier to predict.
So sánh VBA Operators với ví dụ
Bảng sau đây hiển thị So sánh Excel VBA Operators với các ví dụ và đầu ra.
| S / N | Operator | Ví dụ | Đầu ra |
|---|---|---|---|
| 1 | = | Nếu x = z thì | Trả về true nếu chúng bằng nhau, ngược lại trả về false |
| 2 | < | Nếu x < z Thì | Trả về true nếu x nhỏ hơn z, nếu không thì trả về false |
| 3 | > | Nếu x > z thì | Trả về true nếu x lớn hơn z, nếu không thì trả về false |
| 4 | <> | Nếu x <> z Thì | Trả về true nếu chúng không bằng nhau, ngược lại trả về false |
| 5 | <= | Nếu x <= z Thì | Trả về true nếu x nhỏ hơn hoặc bằng z, nếu không nó trả về false |
| 6 | >= | If x >= z Then | Trả về true nếu x lớn hơn hoặc bằng z, nếu không thì trả về false |
Mã nguồn ví dụ
So sánh bằng nhau Operator
If 2 = 1 Then MsgBox "True", vbOKOnly, "Equal Operator" Else MsgBox "False", vbOKOnly, "Equal Operator" End If
ĐÂY,
- “Nếu 2 = 1 Thì… Ngược lại… Kết thúc Nếu” sử dụng câu lệnh if để đánh giá điều kiện “2 = 1”
- “Tin nhắnBox... " Là một hàm tích hợp hiển thị hộp thông báo.
- Tham số đầu tiên “True” hoặc “False” sẽ được hiển thị trong hộp thông báo. Trong ví dụ của chúng tôi, 2 không bằng 1, do đó, nó sẽ hiển thị “false” trong hộp thông báo.
- Tham số thứ hai “vbOKOnly” là nút được hiển thị trong hộp thông báo
- Tham số thứ ba “Bằng Opera“tor” là tiêu đề của hộp tin nhắn.
Thực hiện đoạn mã trên sẽ cho kết quả sau
That single test is the simplest form. Real macros combine comparisons and branch between several outcomes.
How to Use Comparison Operators in an If Statement
A comparison operator is rarely useful on its own. Its True or False result is fed to a decision structure, and the If statement is the one you will use most. The example below grades a score held in cell A1 and writes the outcome next to it.
Sub GradeScore() Dim Score As Long Score = Sheet1.Range("A1").Value If Score >= 90 Then Sheet1.Range("B1").Value = "Distinction" ElseIf Score >= 50 Then Sheet1.Range("B1").Value = "Pass" ElseIf Score < 0 Then MsgBox "A score cannot be negative", vbCritical, "Invalid Entry" Else Sheet1.Range("B1").Value = "Fail" End If End Sub
The macro reads the score once, then tests it against three conditions in order. Four points explain why it behaves the way it does.
- Thứ tự quan trọng: VBA stops at the first branch that returns True. Placing Score >= 50 trước Score >= 90 would label every high score as a pass.
- Else is the catch-all: Any value that fails every earlier test falls through to Else, so no input is left unhandled.
- No chaining: VBA cannot read 0 <= Score <= 100. Viết If Score >= 0 And Score <= 100 Then bằng cách sử dụng toán tử logic thay thế.
- Same test, other structures: The identical condition drives Do While and Select Case, so the operators transfer to every loop you write.
💡 Mẹo: Declare the variable with a numeric type, as shown with Dim Score As Long. If the value stays a Variant holding text, “9” > “10” returns True because the comparison is done character by character.
Comparing Text and Dates in VBA
Numbers compare exactly as arithmetic suggests, but text and dates follow their own rules, and both surprise beginners.
Strings are compared one character at a time using their sort position, so “Apple” < “Banana” returns True. By default the comparison is case sensitive, which means “guru99” = “Guru99” returns False. Placing Tùy chọn So sánh văn bản at the top of the module makes every string comparison in that module ignore case. For a single test without changing the module, use StrComp with the vbTextCompare argument.
Dates are stored as serial numbers counted from 30 December 1899, so they compare directly with the same six operators. Writing If DueDate < Date Then correctly detects an overdue item, because Date returns the current system date as the same kind of serial number. The one trap is a date held as text: “01/02/2026” read from an imported file is a string, not a date, and compares alphabetically. Convert it with CDate before testing it.
Knowing the rules for each type removes most surprises, but a few errors still catch experienced developers.
Common Mistakes When Comparing Values in VBA
The five problems below account for most comparisons that return an unexpected result.
- Assignment mistaken for comparison: Outside a condition, x = 5 stores 5 in x. Inside If, the same symbols test equality. VBA has no separate == operator, so read the context carefully.
- Kiểm tra Double values for exact equality: Stored precision means 0.1 + 0.2 = 0.3 returns False. Compare the rounded difference instead, for example If Abs(a – b) < 0.000001 Then.
- Comparing anything with Null: Null is not equal to anything, not even another Null. Test with IsNull rather than = Null.
- Comparing text against a number: A cell holding “100” as text is not equal to the number 100. Convert with CLng or CDbl before the test.
- Reading .Text instead of .Value: The .Text property returns what is displayed, which is truncated by column width and formatting. Always compare .Value.
Tải xuống tệp Excel ở trên Code

