StrComp VBA function – Compare two texts

Last Updated on January 11, 2023 by token

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.

Constant Value Description
vbBinaryCompare 0 Binary comparison. Case sensitive.
vbTextCompare 1 Text comparison. It is not case sensitive

Function return : Variant

Types of results Return
string1 is shorter than string2 -1
string1 is equal to string2 0
string1 is longer than string2 1
string1 or string2 is empty null

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

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: