What is VBA STRCOMP Function
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).
how to use it
To use VBA's STRCOMP function you need to understand its syntax and arguments:
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.
Example to use STRCOMP Function in VBA
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.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.