Table of contents
Collapse the table of content
Expand the table of content

StrComp Function

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Returns a Variant ( Integer ) indicating the result of astring comparison. SyntaxStrComp ( string1, string2 [, compare ]) The StrComp function syntax has thesenamed arguments:

string1Required. Any valid string expression.
string2Required. Any valid string expression.
compareOptional. Specifies the type of string comparison. If the compareargument isNull, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.

Settings The compare argument settings are:

vbUseCompareOption-1Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare0Performs a binary comparison.
vbTextCompare1Performs a textual comparison.
vbDatabaseCompare2Microsoft Access only. Performs a comparison based on information in your database.

Return Values The StrComp function has the following return values:

IfStrComp returns
string1 is less than string2-1
string1 is equal to string20
string1 is greater than string21
string1 or string2 is NullNull


This 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.

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

© 2018 Microsoft