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?
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.

There is nothing attached?

Martin LissOlder than dirtCommented:
Run the GetInfo sub in Module1.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Shaun VermaakTechnical Specialist/DeveloperCommented:
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
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:
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)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.