Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: String Functions – strComp()

Description: 

The strComp() function compares the two strings and returns the comparison result in numeric form.

Format:

StrComp(string1, string2[, compare])

Arguments:
    • String1
      • Mandatory
      • Type: String
      • First string which gets compared with another one

    • String2
      • Mandatory
      • Type: String
      • Second string which gets compared with another one
    • Compare
      • Optional
      • Type: Numeric
      • The type of comparison to find the string in the main string, like vbBinaryCompare ( Value =0), vbTextCompare (value=1).

       

      OutComes: 
      
      If String1>String2 Then
      
          Result will be 1
      
      Elseif String1<String2 Then
      
          Result will be -1
      
      ElseIf String1=String2 Then
      
          Result will be 0
      
      End If

      Code:

      Function FnStrComp(strString1, strString2)
      
         intResult = StrComp(strString1, strString2, vbTextCompare)    
      
         Select Case intResult
      
         Case 1:
      
          MsgBox strString1 & " string is greater than " & strString2
      
         Case -1:
      
          MsgBox strString1 & " string is less than " & strString2
      
         Case 0:
      
          MsgBox strString1 & " string is equal to " & strString2
      
         End Select
      
      End Function

 

Example 1:

CallFnStrComp(“SUMIT”, “sumit”)

Strcomp() -1

Strcomp() -1

Example2 :

Call FnStrComp(“Sun”, “Moon”)

Strcomp() - 2

Strcomp() – 2

 

Also Read About  Other String() Functions

INSTR()                       |                         InstrREV()                       |                        LCase()

UCase()                      |                         Left()                                |                        Right()

LTrim()                        |                         Mid()                                 |                        Trim()

RTrim()                       |                        Replace()                          |                        Space()

Len()                           |                        StrComp()                         |                        String()

Happy Macro­ing :)

Sumit Jain

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: