StrComp Function

Returns a value indicating the result of a string comparison

Syntax

StrComp(string1, string2[, compare])

Arguments:

  • string1: Required. Any valid string expression.
  • string2: Required. Any valid string expression.
  • Compare: Optional. Numeric value indicating the kind of comparison to use when evaluating strings. If omitted, a binary comparison is performed. See Settings section for values.
  • Settings: The compare argument can have the following values:

    Table 11-21 Comparison Constants and Descriptions

    Constant Value Description
    vbBinaryCompare 0 Perform a binary comparison
    vbTextCompare 1 Perform a textual comparison

Return Value

The StrComp function has the following return values:

Table 11-22 StrComp Function Return Values

If StrComp Returns
string1 is less than string2 -1
string1 is equal to string2 0
string1 is greater than string2 1

Remarks

The following example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed. If the third argument is omitted, system default comparison is performed.

The following example illustrates the use of the StrComp function:

Example 1:

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"   ' Define variables.
MyComp = StrComp(MyStr2, MyStr1)   
' Output: 1

MyComp = StrComp(MyStr1, MyStr2, 1)   
' Output: 0
MyComp = StrComp(MyStr1, MyStr2, 0)   
' Output: -1

Example 2:

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD"
MyStr2 = "XYZ"
MyComp = StrComp(MyStr1, MyStr2)   
' Output: -1

Example 3:

Dim MyStr1, MyStr2, MyComp
MyStr1 = "XYZ"
MyStr2 = "ABCD"
MyComp = StrComp(MyStr1, MyStr2)   
' Output: 1