+ Reply to Thread
Results 1 to 8 of 8

Extracting a number (part of a series of numbers) from a text string

Hybrid View

dannyjoer Extracting a number (part of... 11-08-2012, 10:11 AM
Ron Coderre Re: Extracting a number (part... 11-08-2012, 10:45 AM
dannyjoer Re: Extracting a number (part... 11-08-2012, 11:49 AM
Ron Coderre Re: Extracting a number (part... 11-08-2012, 12:51 PM
tigeravatar Re: Extracting a number (part... 11-08-2012, 01:26 PM
FDibbins Re: Extracting a number (part... 11-08-2012, 01:34 PM
dannyjoer Re: Extracting a number (part... 11-09-2012, 04:44 AM
Ron Coderre Re: Extracting a number (part... 11-09-2012, 10:17 AM
  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Extracting a number (part of a series of numbers) from a text string

    Hi all

    I am working on a project involving a large sparepart catalogue. The dataset coantains a messy and alterning textfile in which a specific sparepart number is found.

    The textfile is contained in a single cell and could look something like this:

    "For T/C NA57 Serial no.1954 Spec. AD44 A4K326;Maker: Mitsui Engineering;1 stk. Clamping Sleeve 596.092 (3C-03049);Indk.pris Eur/stk xxx,xx -25% lev 2 uger ( ref: A 450168 D )"

    or:

    GASKET 517.085 for T/C NA48S SERIES: 1184536;PLANT INFO SIGER:;TURBOCHARGER TYPE NA48/SO1053.;Pris euro/stk xxx,xx -25% lev.tid 2 uger fra ordre;( A443765 B)

    The number i want to extract/copy from the cell to an adjacent cell is the 5xx.xxx number from the lines. That way i wold be able to identify and search the whole catalogue using that unique number.

    Is this possible?
    I have encountered lots of trouble trying to employ my limited excel skills on the problem using the =mid, =left and =len functions.
    The problem is that the number 5 for instance would appear in the cell before Excel hits the 5xx.xxx i want to find, and then copy that number to an adjacent cell.

    Can you guys help? Either by formulas or VBA?

    Thanks alot
    /Danny

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extracting a number (part of a series of numbers) from a text string

    With your text in cell A1
    Example:
    A1: For T/C NA57 Serial no.1954 Spec. AD44 A4K326;Maker: Mitsui Engineering;1 stk. Clamping Sleeve 596.092 (3C-03049);Indk.pris Eur/stk xxx,xx -25% lev 2 uger ( ref: A 450168 D )

    (EDITED: Yikes! Every time I look at this thing I find another section to add)
    This regular formula extracts the first instance of the pattern ###.###
    B1: =MID(A1,MATCH(1,INDEX(ISNUMBER(--(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-6)),3)))*(MID(A1,ROW(INDIRECT("4:"
    &LEN(A1)-3)),1)=".")*ISNUMBER(--(MID(A1,ROW(INDIRECT("5:"&LEN(A1)-2)),3)))*ISNUMBER(--MID(A1,ROW(INDIRECT("1:"
    &LEN(A1)-7)),7))*(LEN(TRIM(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-6)),7)))=7),0),0),7)
    In the above example, that formula returns: 596.092

    Is that something you can work with?
    Last edited by Ron Coderre; 11-08-2012 at 10:59 AM.
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Extracting a number (part of a series of numbers) from a text string

    Hi Ron

    Thx ever so much, i works like a charm.

    Just ran it in a sheet containing 1500 cells containing similar text files as mentioned, and it located the number in 95% of the cases.

    The majority of the "errors" is in the number being seperated by a "-" instead of a "." But i can deal with that just changing the parameter in the code.

    Just to pick your brain;
    Some of the numbers look like this: 5xx.x.xxx, is there an easy way to implement that in the formula as well?

    Best regards

    /Danny

  4. #4
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extracting a number (part of a series of numbers) from a text string

    You're starting to nose into User Defined Function (UDF) territory. The formula I posted is already ugly enough. I suspect the new requirements would double its size. Would you consider using a UDF if we showed you how to add it to your workbook and use it?

    If yes, I'm not an expert with regular expressions, but this seems to work.

    • Select the workbook that will contain the code
    • ALT+F11...to open the vba editor
    • For that workbook....
    ...Insert.Module
    • Copy the below VBA code and paste it into that module
    Function SparePartNum(ByVal text As String) As String
    
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    
    result = "no match"
    
    Set RE = CreateObject("vbscript.regexp")
    
    RE.Pattern = "[0-9]{3}[\.\-][0-9]{3}"
    RE.Global = True
    RE.IgnoreCase = True
    Set allMatches = RE.Execute(text)
    
    If allMatches.Count <> 0 Then
        result = allMatches.Item(0)
    Else
        RE.Pattern = "[0-9]{3}[\.\-]\d[\.\-][0-9]{3}"
        Set allMatches = RE.Execute(text)
        If allMatches.Count <> 0 Then
            result = allMatches.Item(0)
        End If
    End If
    
    SparePartNum = result
    
    End Function
    That UDF returns any of these patterns:
    ###.###
    ###-###
    ###.#.###
    ###-#-###

    Example on howto use that UDF for cell A1...
    =sparepartnum(A1)

    Is that something you can use?

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Extracting a number (part of a series of numbers) from a text string

    Danny,

    Welcome to the forum!
    This looked like an interesting challenge so I gave it a shot. I know Ron has already provided a solution, but here's what I came up.
    If you are always looking for a 5xx.xxx number, then this should work:
    =MID(A1,SEARCH(" 5*.* ",A1)+1,7)
    However, if the number does not always start with 5, and if the length of the number is not always 7 (xxx.xxx) then this will extract it regardless (this formula also converts - into . so that it will still pull the number:
    =TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",255)),MIN(SEARCH(" "&{1,2,3,4,5,6,7,8,9,0}&"*.* ",SUBSTITUTE(SUBSTITUTE(A1&" 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 0.0 ","-",".")," ",REPT(" ",255)))),255))

    As for your later stated requirement of finding a different pattern, I didn't have much luck with that, so hopefully the UDF Ron provided will work for you
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Extracting a number (part of a series of numbers) from a text string

    @ Ron you should carry that thing around in you're pocket (if it will fit) and use it against muggers and the like lol
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  7. #7
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Extracting a number (part of a series of numbers) from a text string

    Thx so much guys, the formulas are solid.

    @Ron, the module works perfect. I'll try to expand it even further.. That VBA thing is really powerful..

    I am very impressed by this forum.

    My problem is solved!
    /Danny

  8. #8
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extracting a number (part of a series of numbers) from a text string

    I'm glad I could help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1