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 str­Comp() func­tion com­pares the two strings and returns the com­par­i­son result in numeric form.

Format:

StrComp(string1, string2[, compare])

Arguments:
    • String1
      • Manda­tory
      • Type: String
      • First string which gets com­pared with another one

    • String2
      • Manda­tory
      • Type: String
      • Sec­ond string which gets com­pared with another one
    • Com­pare
      • Optional
      • Type: Numeric
      • The type of com­par­i­son to find the string in the main string, like vbBina­ryCompare ( Value =0), vbTextCom­pare (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

 

Exam­ple 1:

CallFn­Str­Comp(“SUMIT”, “sumit”)

Strcomp() -1

Str­comp() –1

Example2 :

Call FnStrComp(“Sun”, “Moon”)

Strcomp() - 2

Str­comp() — 2

 

Also Read About  Other String() Functions

INSTR()                       |                         Instr­REV()                       |                        LCase()

UCase()                      |                         Left()                                |                        Right()

LTrim()                        |                         Mid()                                 |                        Trim()

RTrim()                       |                        Replace()                          |                        Space()

Len()                           |                        Str­Comp()                         |                        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: