Saturday, July 23, 2016

How to use the STRCOMP Function (VBA)

Description

The Microsoft Excel STRCOMP function returns an integer value representing the result of a string comparison.

Syntax

The syntax for the STRCOMP function in Microsoft Excel is:
StrComp ( string1, string2 [, compare ] )

Parameters or Arguments

string1 and string2
The two strings to compare to each other.
compare
Optional. This is the type of comparison to perform. The valid choices are:
VBA ConstantValueExplanation
vbUseCompareOption-1Uses option compare
vbBinaryCompare0Binary comparison
vbTextCompare1Textual comparison

Note

  • If string1 is equal to string2, the StrComp function will return 0.
  • If string1 is less than string2, the StrComp function will return -1.
  • If string1 is greater than string2, the StrComp function will return 1.
  • If either string1 or string2 is NULL, the StrComp function will return NULL.

Applies To

  • Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The STRCOMP function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel STRCOMP function examples and explore how to use the STRCOMP function in Excel VBA code:
StrComp ("TechOnTheNet.com", "TechOnTheNet.com")
Result: 0

StrComp ("TechOnTheNet.com", "Abc")
Result: 1

StrComp ("TechOnTheNet.com", "Xyz")
Result: -1

StrComp ("TechOnTheNet.com", NULL)
Result: NULL
For example:
Dim LResult As Integer

LResult = StrComp ("TechOnTheNet.com", "TechOnTheNet.com")
In this STRCOMP example, the variable called LResult would now contain the value 0 since both string values are equal.