totn Excel Functions

MS Excel: How to use the STRCOMP Function (VBA)

This Excel tutorial explains how to use the Excel STRCOMP function with syntax and examples.

Description

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

The STRCOMP function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

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 Constant Value Explanation
vbUseCompareOption -1 Uses option compare
vbBinaryCompare 0 Binary comparison
vbTextCompare 1 Textual comparison

Returns

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 for Office 365, Excel 2019, 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.