• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

User defined function to extract specific string

YasserKhalil

Well-Known Member
Hello everyone
I need UDF that extracts the string that exists in an array
Example :
a=Array("UY","OP","ST")

example of string : "Hello everyone ST exists here" >> expected output would be ST (case sensitive)

Another example : "Hello everyone ST exists here UY too" >> expected output would be UY

Hope that clear
Regards
 
Thanks a lot for reply Mr. Hui
In fact I need that as it is a part of large code so UDF is the best solution for me
 
Hi ,

Something like this :
Code:
Public Function RetrieveMatch(inputrange As Range) As Variant
                Const LOOKFOR = "UY,OP,ST"
                Dim LookForArray As Variant, currpos As Variant
                Dim i As Integer, maxpos As Integer
                Dim matchfound As Boolean
                Dim lookin As String
              
                lookin = " " & inputrange.Value & " "
                LookForArray = Split(LOOKFOR, ",")
                matchfound = False
              
                For i = 0 To UBound(LookForArray)
                    currpos = InStr(1, lookin, " " & LookForArray(i) & " ", vbBinaryCompare)
                    If Not VBA.IsError(currpos) Then
                        If currpos > maxpos Then
                          maxpos = currpos
                          RetrieveMatch = LookForArray(i)
                        End If
                        matchfound = True
                    End If
                Next
              
                If Not matchfound Then RetrieveMatch = CVErr(0)
End Function
Narayan
 
Back
Top