VBA - strComp



The StrComp function returns an integer value after comparing the two given strings. It can return any of the three values -1, 0, or 1 based on the input strings to be compared.

  • If String 1 < String 2, then StrComp returns -1
  • If String 1 = String 2, then StrComp returns 0
  • If String 1 > String 2, then StrComp returns 1

Syntax

StrComp(string1,string2[,compare]) 

Parameter Description

  • String1 − A required parameter. The first string expression.

  • String2 − A required parameter. The second string expression.

  • Compare − An optional parameter. Specifies the string comparison to be used. It can take the following values.

    • 0 = vbBinaryCompare - Performs Binary Comparison(Default)

    • 1 = vbTextCompare - Performs Text Comparison

Example

Add a button and add the following function.

Private Sub Constant_demo_Click()
   Dim var1 as Variant
   msgbox("Line 1 :" & StrComp("Microsoft","Microsoft"))
   msgbox("Line 2 :" &StrComp("Microsoft","MICROSOFT"))
   msgbox("Line 3 :" &StrComp("Microsoft","MiCrOsOfT"))
   msgbox("Line 4 :" &StrComp("Microsoft","MiCrOsOfT",1))
   msgbox("Line 5 :" &StrComp("Microsoft","MiCrOsOfT",0))
End Sub

When you execute the above function, it produces the following output.

Line 1 :0
Line 2 :1
Line 3 :1
Line 4 :0
Line 5 :1 
vba_strings.htm
Advertisements