Your Programming and Traning Professionals

Excel strComp Function: Compare String Values for Matches


StrComp Excel function is a helpful function to use when comparing strings values to identify matches.

 

The Basics:


StrComp ( StringA, StringB, Compare Option)

StringA & StringB = The two strings we want to compare

Compare Option = What compare option we want to use. We can use this option when we want to make sure our string compare is case sensitive or not.


Now let’s look at an example. For this example, we have two columns of data we want to compare to see if they are the same.



Code

Dim A As Long         ’The counter we will be using the process our records

Dim StringA, StringB As String      ‘Strings to compare

Dim Position As Long                    ’This variable will contain the position of the character searched for

For A = 2 To 4

StringA = ActiveSheet.Cells(A, 1).Value   ‘Assigns the value to the first string to compare

StringB = ActiveSheet.Cells(A, 2).Value   ‘Assigns the value to the first string to compare

ActiveSheet.Cells(A, 3).Value = StrComp(StringA, StringB, vbTextCompare)

Next

trim function where needed.

 

Output

A match would have a results value of 0. If the values do not match an -1 is returned in Excel.


Do you want to learn more about Excel and become a PRO in VBA at your own pace? Our friends over at Excel Campus will teach you everything you need to know!