Extract specific string from cell

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Extract specific string from cell

Post by YasserKhalil »

Hello everyone
I have the following string

Code: Select all

XXXXXXXXXXXXXX Name : Yasser - XXXXXXXXXXXX
I need to extract the string "Yasser" .. In fact I can do it using formulas but I need UDF to do that task
The extraction would depend on two delimiters which are "Name :" and the "-"
I mean I need the UDF to deal with the two delimiters and extract what in between

I imagine the following:
=myUDF(A1,"Name : "," - ")

Thanks advanced for help

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Extract specific string from cell

Post by HansV »

Here you go:

Code: Select all

Function MyUDF(s As String, b As String, a As String) As String
    Dim arr() As String
    Dim r As String
    arr = Split(s, b)
    If UBound(arr) > 0 Then
        r = arr(1)
        arr = Split(r, a)
        If UBound(arr) > 0 Then
            r = arr(0)
        End If
    End If
    MyUDF = Trim(r)
End Function
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Extract specific string from cell

Post by YasserKhalil »

That's exactly what I was searching for
What a fantastic solution Mr Hans!

Thank you very much for all these unique and great codes