Overriding the Default String-Comparison Setting

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

To perform a string comparison within a procedure and override the string-comparison setting for the module, you can use the StrComp function. The StrComp function takes two strings as arguments, along with a compare argument, which you can use to specify the type of comparison. The possible settings for the compare argument are vbBinaryCompare, vbTextCompare, and (in Microsoft® Access) vbDatabaseCompare. If you omit this argument, the StrComp function uses the module's default comparison method.

The following table lists the possible return values for the StrComp function.

If Then StrComp returns
string1 < string2 -1
string1 = string2 0
string1 > string2 1
string1 Or string2 Is Null Null

For example, running the following code from the Immediate window prints "1", indicating that the ANSI value of the first character in the first string is greater than the ANSI value of the first character in the second string:

? StrComp("vba", "VBA", vbBinaryCompare)

On the other hand, if you specify text-based string comparison, this code prints "0", indicating that the two strings are identical:

? StrComp("vba", "VBA", vbTextCompare)

Other Microsoft® Visual Basic® for Applications (VBA) string functions that perform string comparison also provide a compare argument that you can use to override the default string-comparison setting for that function call. For example, the InStr and InStrRev functions both have a compare argument.

See Also

Getting the Most Out of Visual Basic for Applications | Comparing Strings | Specifying the String-Comparison Setting for a Module | Comparing Strings by Using Comparison Operators