VBA String Comparison | How to Compare Two String Values? (2024)

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.

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?

Table of contents
  • Excel VBA String Comparison
    • How to Perform String Comparison in VBA?
      • Example #1
      • Example #2
      • Example #3
    • Things to Remember here
    • Recommended Articles
VBA String Comparison | How to Compare Two String Values? (1)

Below is the syntax of the “StrComp” function.

VBA String Comparison | How to Compare Two String Values? (2)

First, two arguments are quite simple.

  • 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.
  • 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 StringEnd Sub
VBA String Comparison | How to Compare Two String Values? (3)

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 Sub
VBA String Comparison | How to Compare Two String Values? (4)

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 StringEnd Sub
VBA String Comparison | How to Compare Two String Values? (5)

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 Sub
VBA String Comparison | How to Compare Two String Values? (6)

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 Sub
VBA String Comparison | How to Compare Two String Values? (7)

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
VBA String Comparison | How to Compare Two String Values? (8)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (9)

Let us run the code and see the result.

Output:

VBA String Comparison | How to Compare Two String Values? (10)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (11)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (12)

Now, run the code and see the result.

Output:

VBA String Comparison | How to Compare Two String Values? (13)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (14)

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

Output:

VBA String Comparison | How to Compare Two String Values? (15)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (16)

Run the code and see the result.

Output:

VBA String Comparison | How to Compare Two String Values? (17)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (18)

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 FinalResultEnd Sub
VBA String Comparison | How to Compare Two String Values? (19)

Run the code and see the result.

Output:

VBA String Comparison | How to Compare Two String Values? (20)

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

Things to Remember here

  • 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.

Recommended Articles

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: -

  • Guide to VBA String Functions
  • VBA Split String into Array
  • VBA SubString Methods
  • VBA Text
VBA String Comparison | How to Compare Two String Values? (2024)
Top Articles
5 Types of Digital Media
Jingle Bulls: Holiday Stock Market Trends you Need to Watch Now
Dainty Rascal Io
Parke County Chatter
My E Chart Elliot
122242843 Routing Number BANK OF THE WEST CA - Wise
Login Page
Shaniki Hernandez Cam
Irving Hac
Zachary Zulock Linkedin
shopping.drugsourceinc.com/imperial | Imperial Health TX AZ
Uvalde Topic
5808 W 110Th St Overland Park Ks 66211 Directions
No Strings Attached 123Movies
Bowlero (BOWL) Earnings Date and Reports 2024
Current Time In Maryland
Walmart End Table Lamps
Minecraft Jar Google Drive
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Rondom Ajax: ME grijpt in tijdens protest Ajax-fans bij hoofdbureau politie
Air Force Chief Results
Johnnie Walker Double Black Costco
Panolian Batesville Ms Obituaries 2022
Free T33N Leaks
Till The End Of The Moon Ep 13 Eng Sub
Nurtsug
County Cricket Championship, day one - scores, radio commentary & live text
James Ingram | Biography, Songs, Hits, & Cause of Death
Devotion Showtimes Near The Grand 16 - Pier Park
Missing 2023 Showtimes Near Mjr Southgate
Ripsi Terzian Instagram
2430 Research Parkway
Martin Village Stm 16 & Imax
Desirulez.tv
Exploring The Whimsical World Of JellybeansBrains Only
Darrell Waltrip Off Road Center
No Hard Feelings Showtimes Near Tilton Square Theatre
SOC 100 ONL Syllabus
Baywatch 2017 123Movies
Chuze Fitness La Verne Reviews
Hebrew Bible: Torah, Prophets and Writings | My Jewish Learning
Toth Boer Goats
Lamp Repair Kansas City Mo
Fool's Paradise Showtimes Near Roxy Stadium 14
6576771660
Craigslist Minneapolis Com
Blue Beetle Showtimes Near Regal Evergreen Parkway & Rpx
Greg Steube Height
N33.Ultipro
Evil Dead Rise - Everything You Need To Know
Cvs Minute Clinic Women's Services
2487872771
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5732

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.