Be the first user to complete this post

  • 0
Add to List

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



Also Read:

  1. VBA-Excel: Date-Time Functions – Hour(), Minute(), Second()
  2. VBA Excel - Cells, Ranges and Offset: Refer to Cells by Using Shortcut Notation
  3. VBA-Excel: Fill Excel Range Values in a 2D Array
  4. VBA-Excel: Array Functions – LBound and UBound()
  5. VBA-Excel: String Functions – Len()