VBA STRCOMP Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

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

Syntax

StrComp(String1, String2, [Compare])

Arguments

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

Example

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:

example-vba-strcomp-function
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.