Elite Membership

VBA String Comparison

Written by WallStreetMojo Team WallStreetMojo Team Content Writers & Editors Our team of international writers & editors do in-depth research and fact check while creating informative articles, covering all aspects from basic to advanced. They employ their expertise to bring best possible content to our readers. 20+ years of experience Finance Excel View Full Profile
Reviewed by Dheeraj Vaidya, CFA, FRM Dheeraj Vaidya, CFA, FRM Content Reviewer & Course Director A former J.P.Morgan and CLSA Equity Analyst, Dheeraj specializes in financial modeling, AI, forecasting, and valuations. In his career spanning almost two decades, he has trained and mentored more than 100,000 students and professionals on a range of topics. 20+ years of experience CFA, FRM, IIT Delhi, IIM Lucknow Financial Modeling View Full Profile
Updated Dec 23, 2024
Read Time 5 min

Excel VBA String Comparison

We have a built-in function to compare two strings in VBA: โ€œStrComp.โ€ We can read it as โ€œString Comparison.โ€ This function is available only with VBA and not as a Worksheet function. It compares any two strings and returns the results as โ€œZero (0)โ€ if both strings match. We will get โ€œOne (1)โ€ if both supplied strings do not match.

Download FREE VBA String Comparison Template and Follow Along!
Download Excel Template

In VBA or Excel, we face plenty of different scenarios. One such scenario is comparing two string values. Of course, we can do these in a regular worksheet multiple ways, but how do you do this in VBA?

VBA String Comparison

Below is the syntax of the โ€œStrCompโ€ function.

VBA String Comparison Syntax

First, two arguments are quite simple.

Key Takeaways

  • For String 1, we need to supply the first value we are comparing.
  • For String 2, we need to supply the second value we are comparing.
  • [Compare] this is the optional argument of the StrComp function. It is helpful when we want to compare case-sensitive comparisons. For example, in this argument, โ€œExcelโ€ is not equal to โ€œEXCELโ€ because both these words are case sensitive.

We can supply three values here.

  • Zero (0) for โ€œBinary Compare,โ€ i.e., โ€œExcel,โ€ is not equal to โ€œEXCEL.โ€ For case-sensitive comparison, we can supply 0.
  • One (1) for โ€œText Compare,โ€ i.e., โ€œExcel,โ€ is equal to โ€œEXCEL.โ€ It is a non-case-sensitive comparison.
  • Two (2) this only for database comparison.

The results of the โ€œStrCompโ€ function do not default TRUE or FALSE but vary. Below are the different results of the โ€œStrCompโ€ function.

  • We will get โ€œ0โ€ if the supplied strings match.
  • We will get โ€œ1โ€ if the supplied strings are not matching. In the case of numerical matching, we will get 1 if String 1 is greater than string 2.
  • We will get โ€œ-1โ€ if the string 1 number is less than the string 2 number.

How to Perform String Comparison in VBA?

Example #1

We will match โ€œBangaloreโ€ against the string โ€œBANGALORE.โ€

But, first, declare two VBA variables as the string to store two string values.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String End SubVBA String Comparison Example 1

For these two variables, store two string values.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” End SubExample 1.1

Now, declare one more variable to store the result of the โ€œStrCompโ€ function.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String End SubVBA String Comparison Example 1.2

For this variable, open the โ€œStrCompโ€ function.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String ย FinalResult = StrComp( End SubVBA String Comparison Example 1.3

We have already assigned values through variables for โ€œString1โ€ and โ€œString2,โ€ so enter variable names, respectively.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, End SubVBA String Comparison Example 1.4

The last part of the function is โ€œCompareโ€ for this choice โ€œvbTextCompare.โ€

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbTextCompare) End Sub Example 1.5

Now show theย โ€œFinal Resultโ€ย variable in the message box in VBA.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbTextCompare) ย MsgBox FinalResult End SubVBA String Comparison Example 1.6

Let us run the code and see the result.

Output:

 Example 1.7

Since the strings โ€œBangaloreโ€ and โ€œBANGALOREโ€ are the same, we got the result as 0, i.e., matching. However, both the values are case-sensitive since we have supplied the argument as โ€œvbTextCompare,โ€ it has ignored the case-sensitive match and matched only values, so both the values are the same, and the result is 0, i.e., TRUE.

Code:

Sub String_Comparison_Example1() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbTextCompare) ย MsgBox FinalResult End SubVBA String Comparison Example 1.8

Example #2

We will change the compare method for the same code from โ€œvbTextCompareโ€ to โ€œvbBinaryCompare.โ€

Code:

Sub String_Comparison_Example2() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = “Bangalore” ย Value2 = “BANGALORE” ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbBinaryCompare) ย MsgBox FinalResult End Sub Example 2

Now, run the code and see the result.

Output:

VBA String Comparison Example 2.1

Even though both the strings are the same, we got the result as 1, i.e., not matching because we have applied the compare method as โ€œvbBinaryCompare,โ€ which compares two values as case sensitive.

Example #3

Now, we will see how to compare numerical values. For the same code, we will assign different values.

Code:

Sub String_Comparison_Example3() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = 500 ย Value2 = 500 ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbBinaryCompare) ย MsgBox FinalResult End SubVBA String Comparison Example 3

Both the values are 500. Therefore, we will get 0 as a result because both the values match.

Output:

Example 3.1

Now, we will change the Value1 number from 500 to 100.

Code:

Sub String_Comparison_Example3() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = 1000 ย Value2 = 500 ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbBinaryCompare) ย MsgBox FinalResult End SubVBA String Comparison Example 3.2

Run the code and see the result.

Output:

 Example 3.3

We know Value1 and Value2 are not the same. But the result is -1 instead of 1 because for numerical comparison, when the String 1 value is greater than String 2, we will get this -1.

Code:

Sub String_Comparison_Example3() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = 1000 ย Value2 = 500 ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbBinaryCompare) ย MsgBox FinalResult End SubVBA String Comparison Example 3.4

Now, we will reverse the values.

Code:

Sub String_Comparison_Example3() ย Dim Value1 As String ย Dim Value2 As String ย Value1 = 500 ย Value2 = 1000 ย Dim FinalResult As String ย FinalResult = StrComp(Value1, Value2, vbBinaryCompare) ย MsgBox FinalResult End Sub Example 3.5

Run the code and see the result.

Output:

VBA String Comparison Example 3.6

It is not special. If it does not match, we will get only 1.

Things to Remember here

  • [Compare] argument of โ€œStrCompโ€ is optional, but in case of case sensitive match, we can utilize this, and the option is โ€œvbBinaryCompare.โ€
  • The result of numerical values is slightly different if String 1 is greater than string 2, and the result will be -1.
  • The results are 0 if matched and 1 if not matched.

This article has been a guide to the VBA string comparison. Here, we discuss comparing two string values using the StrComp function in Excel VBA and examples and downloading an Excel template. You may also have a look at other articles related to Excel VBA: –