How to use the VBA STRCOMP Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA STRCOMP Function (Syntax + Example)

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 (with the 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 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 the cell A1 and cell A2 and it has returned 1 in the result as both of the values are not the same.

Related Functions

FORMAT | INSTR | INSTRREV | LCASE | LEFT | LEN | LTRIM | MID | REPLACE | RIGHT | RTRIM | SPACE | STRCONV | STRING | STRREVERSE | TRIM | UCASE