VBA STRCOMP Function (Syntax + Example)


- Written by Puneet

The VBA STRCOMP function is listed under the text category of VBA functions. When you use it in a VBA code, it returns an integer after comparing two strings with each other. In simple words, you compare two strings with it using it (in three different ways).


StrComp(String1, String2, [Compare])


  • String1: The first string to compare.
  • String2: The second string to compare.
  • [Compare]: A string value to define the type of String comparison to make [This is an optional argument and if omitted VBA takes vbBinaryCompare by default].
    • vbBinaryCompare: For binary comparison.
    • vbTextCompare: For text comparison.
    • vbDatabaseCompare: For Database Comparison.


To practically understand how to use the VBA STRCOMP function, you need to go through the below example where we have written a vba code by using it:

Sub example_STRCOMP()
Range("B1").Value = StrComp(Range("A1"), Range("A2"))    
End Sub

In the above code, we have used the STRCOMP to compare the values from cell A1 and cell A2 and it has returned 1 in the result as both of the values are not the same.

Last Updated: June 22, 2023