VBA StrComp function

StrComp Function Description

The VBA StrComp function compares two strings, and returns the result of this comparison as an Integer representing the result of a string comparison.

Syntax

The syntax for the StrComp function in VBA is:

StrComp ( string1, string2 [, compare ] )

Parameters

string1
First string to be compared.

string2
Second string to be compared

compare
Optional. The type of comparison to be performed. VBA Constant values:

Constant Value Description
vbUseCompareOption -1 Uses option compare
vbBinaryCompare 0 Binary comparison (distinguishes letter case)
vbTextCompare 1 Textual comparison (ignores letter case)

Other Notes

Condition StrComp Function Result
string1 sorts ahead of string2 -1
string1 is equal to string2 0
string1 sorts after string2 1
string1 or string2 is equal to Null Null

Example usage

The StrComp function can be used in VBA code. Let’s look at some VBA StrComp function examples:

StrComp "Hello", "Hello"
'Result: 0 - string1 is equal to string2

StrComp "Hello", "hello"
'Result: -1 - string1 sorts ahead of string2

StrComp "Abc", "Bcd"
'Result: -1 - string1 sorts ahead of string2

StrComp "Bcd", "Abc"
'Result: 1 - string2 sorts ahead of string1

StrComp "Hello", Null
'Result: Null