比較常數

下表說明常數及其描述:

表格 11-5 比較常數與描述

常數 描述
vbBinaryCompare 0 執行二進位比較
vbTextCompare 1 執行文字比較

下列範例說明比較常數的用法:

範例 1

Dim result
result = StrComp("Hello", "hello", vbBinaryCompare)
 
If result = 0 Then
    'Logic Strings are equal (Binary Comparison)
Else
    'Logic Strings are NOT equal (Binary Comparison)
End If
'Output -> Strings are Not equal (Binary Comparison) Logic

範例 2

Dim result
result = StrComp("Hello", "hello", vbTextCompare)
 
If result = 0 Then
    'Logic Strings are equal (Text Comparison)
Else
    'Logic Strings are NOT equal (Text Comparison)
End If
 
'Output -> Strings are equal (Text Comparison) Logic

範例 3

Dim str1, str2
 
str1 = "Apple"
str2 = "apple"
 
If StrComp(str1, str2, vbTextCompare) < 0 Then
    'Logic If
Else
    'Logic Else 
End If
 
'Output -> apple comes before Apple (Binary) ( Logic Else )