Find data in string of text

joelkoch

New Member
Joined
Aug 28, 2013
Messages
10
I have a column of cells that have a bunch of text in them. in that text there are serial numbers I would like to extract. A cell could have multiple different serial numbers that need to be extracted. For example a cell could have: "Exchange + PS43138 Exchange + PS43178" I'm interested in trying to get both the serial numbers out PS43138 and PS43178. the serial numbers differ slightly, but they all have a 5 digit string of numbers. I tried to create a UDF but can'f figure out how to get all serial numbers out, I can only get the first one. This is the UDF I got from the looking through the forum.

Code:
Function FiveDigitNo(s As String) As StringWith CreateObject("VBScript.RegExp")
  .Pattern = "(?:^|\D)(\d{5})(?!\d)"
  If .Test(s) Then FiveDigitNo = .Execute(s)(0).SubMatches(0)
End With
End Function

Any help would be much appreciated.

Thanks,
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Are they all PS prefix? Are the serial numbers always after a + sign? Does "Exchange" appear in each row?
 
Upvote 0
Unfortunately the only consistent between them is that there is a 5 digit number sequence at the end.
 
Upvote 0
Do all the 5-digit numbers begin with some 2-character prefix?
 
Upvote 0
So, does that mean you want the 5-digit number prefixed with what is before it but after the 1st space (actually, the 2nd space from the beginning of the string)?
 
Upvote 0
I there would be a way to pull out all the 5 digit numbers and put those in a cell separated by a space that would work. Like in the example the formula could extract the two 5 digit numbers in a new cell like this "43138 43178". Ideally putting them in separate cells would be best, but that is just extra steps. Honestly they don't even need to be separated by a space because I believe I could just text to columns them apart anyway. As far as I can see those 5 digit numbers appear to be unique so they are all I really need to do my analysis.

 
Upvote 0
Try something like this

Code:
Dim Counter As Long, STR_5 As String, Original_String As String


Original_String = 'set equal to range or cell where the original string is found or equal to another variable
 
For Counter = 1 To Len(Original_String)




    If IsNumeric(Mid(Original_String, Counter, 1)) Then
    
        STR_5 = Mid(Original_String, Counter-2, 7)
        
        'now ouput STR_5 where you want it to be
        
        Counter = Counter + 4
        
    End If


Next Counter
 
Last edited:
Upvote 0
Thank you for the reply.

I'm having trouble getting the VBA to work. If the name of the Sheet is called "Data" and the serial number are in column H and the output should go in column T how would I write this? Thank you for your help on this.
 
Upvote 0
With your text strings in A1:A?, try this which should put the numbers in the B column.

Code:
Sub ExtractNumbers()

    Dim i As Long, j As Long
    Dim xCell As Range
    Dim xRg As Range
    Dim xTxt As String
    Dim xStr As String
    Dim xUpdate As Boolean
    Dim LR As Long
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    For j = 1 To LR
    Range("B" & j).FormulaArray = "=TEXTJOIN(,1,TEXT(MID(A" & j & ",ROW($A$1:INDEX($A$1:$A$1000,LEN(A" & j & "))),1),""#;-#;0;""))"
    Next j
    On Error Resume Next
    xTxt = ActiveWindow.RangeSelection.Address
    Set xRg = Range("$B$1:$B$" & LR)
    Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)
    If xRg Is Nothing Then Exit Sub
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    For Each xCell In xRg
        
        xStr = xCell.Text
        xTxt = ""
        For i = 1 To Len(xStr) Step 5
            If xTxt = "" Then
                xTxt = Mid(xStr, i, 5)
            Else
                xTxt = Trim(xTxt) & " " & Mid(xStr, i, 5)
            End If
        Next
        xCell = xTxt
    Next
    Application.ScreenUpdating = xUpdate
End Sub

BTW: This assumes you are running the latest version of Excel (e.g., 365) with TEXTJOIN as a function.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top