StrComp VBA function – Compare two texts

1. StrComp VBA Function – Description

The StrComp VBA function   compares the length of strings in two texts. Depending on the difference in length, the function returns the values ​​1, 0 or -1.

2. VBA StrComp Function – Syntax

StrComp (String1, String2, [Compare As VbCompareMethod = vbBinaryCompare])

String1 : The first string we want to compare.

String2 : The second string we want to compare.

Compare : An optional argument. A method for comparing strings. This argument also determines whether the search is case-sensitive.

vbBinaryCompare0Binary comparison. Case sensitive.
vbTextCompare1Text comparison. It is not case sensitive

Function return : Variant

Types of resultsReturn
string1 is shorter than string2-1
string1 is equal to string20
string1 is longer than string21
string1 or string2 is emptynull

3. VBA StrComp function – Example

How to use StrComp  function  in VBA Excel? The following are examples of using the StrComp function in the VisualBasic Editor.

Example 1 : An example of using the StrComp VBA function – result in the MsgBox window.

Dim StrValue As String
StrValue1 = "Learning VBA functions"
StrValue2 = "learning VBA"
MsgBox StrComp (StrValue1, StrValue2, vbTextCompare)
MsgBox StrComp (StrValue2, StrValue1)
Excel VBA Functions - StrComp VBA function
Excel VBA Functions – StrComp VBA function

4. VB StrComp function – Additional information

  • In the third argument of the function, we can introduce a method of comparing values ​​in strings.

5. StrComp VisualBasic function – Where to use?

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The article is part of the VBA Excel function list. You can find a list of all VBA functions at this address:  VBA functions .

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: