การเปรียบเทียบ VBA Operaทอร์: ไม่เท่ากัน Less กว่าหรือเท่ากับ

⚡ สรุปอย่างชาญฉลาด

การเปรียบเทียบ 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.

  • 🇧🇷 วัตถุประสงค์: Comparison operators validate data by testing a condition that evaluates to True or False.
  • 🔢 หก Operaทอร์: Equal, less than, greater than, not equal to, less than or equal to, and greater than or equal to.
  • 🔁 บทบาทคู่: The equals sign compares inside a condition and assigns a value outside one.
  • 🧪 If Statement: Every comparison is placed inside If, ElseIf, or Do While to control which code runs.
  • 🔤 Text and Dates: Strings compare character by character and dates compare as serial numbers.
  • ⚠️ Precision Trap: สอง Double values that look identical can fail an equality test, so compare a rounded difference.

การเปรียบเทียบ VBA Operaโปร

การเปรียบเทียบ VBA Operaโปร

These are operators that are used to compare values. Comparison operators include equal to, less than, greater than and not equal to.

ตัวดำเนินการเปรียบเทียบใช้สำหรับเปรียบเทียบค่าเพื่อวัตถุประสงค์ในการตรวจสอบความถูกต้อง สมมติว่าคุณกำลังพัฒนา...ping นี่คือแอปพลิเคชันจุดขายแบบง่ายๆ ในแอปพลิเคชันนี้ คุณต้องการตรวจสอบความถูกต้องของค่าที่ป้อนก่อนทำการบันทึก ในกรณีเช่นนี้ คุณสามารถใช้ตัวดำเนินการเปรียบเทียบได้ ตัวดำเนินการนี้จะตรวจสอบกับตัวเลขติดลบ หรือเพื่อให้แน่ใจว่าจำนวนเงินที่ชำระไม่เกินจำนวนเงินที่เรียกเก็บ ตัวดำเนินการเปรียบเทียบมีประโยชน์มากในสถานการณ์เช่นนี้

Every comparison returns one of two values, True or False, and never a number. That result is what an If statement acts on.

ตารางต่อไปนี้จะแสดงตัวดำเนินการเปรียบเทียบที่กำหนดไว้ใน VBA.

OperaTor Descriptไอออน
= เท่ากัน: ตรวจสอบว่าค่าสองค่าเท่ากันหรือไม่ นอกจากนี้ยังใช้เป็นตัวดำเนินการกำหนดค่าอีกด้วย
< 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.

การเปรียบเทียบ VBA Operaทอร์พร้อมตัวอย่าง

ตารางต่อไปนี้แสดงการเปรียบเทียบ Excel VBA Operaทอร์พร้อมตัวอย่างและเอาต์พุต

S / N OperaTor ตัวอย่าง เอาท์พุต
1 = ถ้า x = z แล้ว คืนค่าเป็นจริงหากเท่ากัน มิฉะนั้นจะส่งคืนค่าเท็จ
2 < ถ้า x < z แล้ว คืนค่าเป็นจริงถ้า x น้อยกว่า z มิฉะนั้นจะส่งคืนค่าเท็จ
3 > ถ้า x > z แล้ว คืนค่าเป็นจริงถ้า x มากกว่า z มิฉะนั้นจะส่งคืนค่าเท็จ
4 <> ถ้า x <> z แล้ว คืนค่าเป็นจริงหากค่าไม่เท่ากัน มิฉะนั้นจะส่งคืนค่าเท็จ
5 <= ถ้า x <= z แล้ว คืนค่าเป็นจริงถ้า x น้อยกว่าหรือเท่ากับ z มิฉะนั้นจะส่งคืนค่าเท็จ
6 >= If x >= z Then คืนค่าเป็นจริงถ้า x มากกว่าหรือเท่ากับ z มิฉะนั้นจะส่งคืนค่าเท็จ

ตัวอย่างซอร์สโค้ด

การเปรียบเทียบที่เท่าเทียมกัน OperaTor

If 2 = 1 Then
    MsgBox "True", vbOKOnly, "Equal Operator"
Else
    MsgBox "False", vbOKOnly, "Equal Operator"
End If

ที่นี่

  • “ถ้า 2 = 1 แล้ว… อย่างอื่น… สิ้นสุดถ้า” ใช้คำสั่ง if เพื่อประเมินเงื่อนไข “2 = 1”
  • “ข่าวสารเกี่ยวกับBox... " เป็นฟังก์ชั่นในตัวที่แสดงกล่องข้อความ
    • พารามิเตอร์แรก “True” หรือ “False” คือสิ่งที่จะแสดงในกล่องข้อความ ในตัวอย่างของเรา 2 ไม่เท่ากับ 1 ดังนั้นจะแสดง “false” ในกล่องข้อความ
    • พารามิเตอร์ที่สอง “vbOKOnly” คือปุ่มที่แสดงในกล่องข้อความ
    • พารามิเตอร์ที่สาม “เท่ากับ Opera“tor” คือหัวเรื่องของกล่องข้อความ

การดำเนินการโค้ดข้างต้นให้ผลลัพธ์ดังต่อไปนี้

เท่ากัน OperaTor

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.

  • ความสำคัญของการสั่งซื้อ: VBA stops at the first branch that returns True. Placing คะแนน >= 50 ก่อน คะแนน >= 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. เขียน If Score >= 0 And Score <= 100 Then โดยใช้โปรแกรม ตัวดำเนินการเชิงตรรกะ แทน.
  • Same test, other structures: The identical condition drives Do While and Select Case, so the operators transfer to every loop you write.

💡 เคล็ดลับ: 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 ตัวเลือกเปรียบเทียบข้อความ 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.
  • การทดสอบ 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.

ดาวน์โหลดไฟล์ Excel ด้านบน Code

คำถามที่พบบ่อย

No. VBA evaluates the first comparison to True or False, then compares that result with 10, which gives a misleading answer. Write the two tests separately and join them with And.

Like compares a string against a pattern rather than an exact value. Wildcards include the asterisk for any characters and the question mark for one character, so “Guru99” Like “Guru*” returns True.

Use the Is operator, as in If ws Is Nothing Then. The equals sign compares values, while Is checks whether two object variables point at the same object, which is the only valid test for objects.

Yes. Describe the rule in plain words, such as flag any order above 500 placed before today, and an AI assistant returns the matching If condition. Check the boundary values before trusting it.

Yes. Paste the If block and an AI assistant traces which branch catches each value, then points out an unreachable ElseIf or a comparison between text and a number. Confirm the fix by stepping through the code.

สรุปโพสต์นี้ด้วย: