Excel strComp Function

 

Excel StrComp Function

 

The 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

 

 

Note: The vbTextCompare compare option makes sure that the matching is not going to be case sensitive in Excel. The option vbBinaryCompare will ensure that the comparison is case sensitive. It is also important to remember that you might want to remove leading and trailing spaces when doing comparisons. So be sure to utilize the 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.

 

 

 

 

 
Sitemap | Privacy | Terms
Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation.
Copyright 2011 ExcelHelp.com All rights reserved
More Info (+)