Extract variable text/numbers within string in Excel

Hello Experts!

Attached is a small sample of some data I'm working with. Column E [highlighted] is the issue. I need to extract Ticket Number and Passenger Name from among the other data in those cells. I have tried a few helper columns in another file but realized quickly that the Ticket Number lengths are inconsistent. Also, the use of the term Ticket Number also appears as TKT NO. and TKT#.

I have been using variations on MID, TRIM, & FIND but can't crack the ticket length problem. Would truly appreciate any help you might give.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


There is nothing attached?

Martin LissOlder than dirtCommented:
Run the GetInfo sub in Module1.
Shaun VermaakTechnical SpecialistCommented:
Yes you can do that with a RegEx function
Function simpleCellRegex(Myrange As Range) As String
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String

    strPattern = "^[0-9]{1,3}"

    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            simpleCellRegex = regEx.Replace(strInput, strReplace)
            simpleCellRegex = "Not matched"
        End If
    End If
End Function

Open in new window


Or if you are feeling adventurous via this process and any RegEx editor
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

xllvrAuthor Commented:
@Martin Liss - Thank you for your response and attachment. Your code is nearly perfect. Your file is attached with the areas in question highlighted in yellow. There are some instances of ticket numbers with both numbers and letters. Your code is only pulling in numbers. There are also occurrences where the passenger name isn't extracted. Last, when I use the code on my larger data set, some of the ticket numbers are displayed as like this example 5.26142E+12 but look accurate in the formula bar. I think that I might just need to use TRIM there. Will you be able to modify the code to accommodate what I've highlighted?  29092442.xlsm

@Shaun Vermaak - Thanks so much for weighing in. I am completely unfamiliar with RegEx functions but will look into it further. I'm not certain I'll be able to translate it to my specific situation with ease but am up for learning more.
Martin LissOlder than dirtCommented:
OK, for the ticket number I'm now making the assumption that it can't contain any spaces. For the passenger name I'm now making the assumption that if the data doesn't contain "Passenger Name" that it will contain "TKT#" and that the lines before "TKT#" are separated by line feed characters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
some of the ticket numbers are displayed as like this example 5.26142E+12 but look accurate
Format the entire column as Text and rerun the macro.
xllvrAuthor Commented:
This is great! Thank you for making those adjustments. Very much appreciated!
Martin LissOlder than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.