+ Reply to Thread
Results 1 to 10 of 10

How to extract number before and after "-"

  1. #1
    Registered User
    Join Date
    12-22-2015
    Location
    SG
    MS-Off Ver
    2010
    Posts
    40

    How to extract number before and after "-"

    Dear all

    I have
    SUSAR No. 218-239 >>> I want 239 - 218 = 21
    SUSAR No. 1218-1239 >>>I want 1239 - 1218 = 21
    SUSAR No. 918-1039 >>>I want 1039 - 918 = 121

    There is a variety of the # of digits. And I need to take bigger value - smaller value

    How do I write the formula

    Thank you

  2. #2
    Forum Expert
    Join Date
    02-10-2019
    Location
    Georgia, USA
    MS-Off Ver
    Office 365
    Posts
    2,848

    Re: How to extract number before and after "-"

    Well, this uses a LOT of assumptions, like:
    Your numbers always have a dash (-) between them and no spaces between them (and no other dash in the cell). There is always only 1 period and a space before the first number. If this is true, if "SUSAR No. 218-239" was in cell a1, in cell b1 you can use this formula:
    =MAX(NUMBERVALUE(RIGHT(A1,LEN(A1)-(SEARCH("-",A1,1)))),NUMBERVALUE(MID(A1,SEARCH(".",A1,1)+1,SEARCH("-",A1,1)-SEARCH(".",A1,1)-1)))-MIN(NUMBERVALUE(RIGHT(A1,LEN(A1)-(SEARCH("-",A1,1)))),NUMBERVALUE(MID(A1,SEARCH(".",A1,1)+1,SEARCH("-",A1,1)-SEARCH(".",A1,1)-1)))

    I have no doubt this formula can be simplified, but it works (as long as assumptions are correct.

  3. #3
    Registered User
    Join Date
    12-22-2015
    Location
    SG
    MS-Off Ver
    2010
    Posts
    40

    Re: How to extract number before and after "-"

    Yes there is be no spaces between the digit and the "-"
    also the number will be before and after the 1st "-". wonder if there is simplier formulae?
    Thanks Gregb11

  4. #4
    Forum Expert
    Join Date
    10-15-2018
    Location
    MA, USA
    MS-Off Ver
    2010, 2019
    Posts
    1,616

    Re: How to extract number before and after "-"

    Same assumptions as Gregb11.

    Please try in B1:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Geoff

    Did I help significantly? If you wish, click on * Add Reputation to say thanks.
    If your problem has been resolved please select ?Solved? from the Thread Tools menu

  5. #5
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,923

    Re: How to extract number before and after "-"

    Try:

    =RIGHT(A1,LEN(A1)-FIND("-",A1))-(MID(SUBSTITUTE(SUBSTITUTE(A1,"No.","-"),"-",REPT(" ",100)),100,100))

  6. #6
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,213

    Re: How to extract number before and after "-"

    Please try
    =SUM(MID(RIGHT(SUBSTITUTE(SUBSTITUTE(A1,"-"," ")," ",REPT(" ",40)),80),{1,41},40)*{-1,1})

  7. #7
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,453

    Re: How to extract number before and after "-"

    And how about the good old hidden EVALUATE function ?
    Open the Name manager - Enter a name (test, say) -Enter the following formula in "refers to" :=ABS(EVALUATE(TRIM(SUBSTITUTE(Sheet1!$A1,"SUSAR No.","")))) - Close the wizard
    Now enter =test in B1, say, and pronto, that's it.
    Your wbk MUST be saved as xlsm, but you do NOT need to bother with VBA.

    NB I know it's not as impressing as the above formulas, I like the KISS method

  8. #8
    Forum Expert
    Join Date
    02-10-2019
    Location
    Georgia, USA
    MS-Off Ver
    Office 365
    Posts
    2,848

    Re: How to extract number before and after "-"

    Besides Pepe's solution, the other above solutions assume the first number is less than the second but I was under the impression that they could be in either order. If that assumption is true, you'll need to wrap those formulas with min and max's like I have in mine.

  9. #9
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,213

    Re: How to extract number before and after "-"

    Just wrap with ABS to get positive number
    =ABS(SUM(MID(RIGHT(SUBSTITUTE(SUBSTITUTE(A1,"-"," ")," ",REPT(" ",40)),80),{1,41},40)*{-1,1}))

  10. #10
    Forum Expert
    Join Date
    02-10-2019
    Location
    Georgia, USA
    MS-Off Ver
    Office 365
    Posts
    2,848

    Re: How to extract number before and after "-"

    Yes, I thought of that right after I posted - much better

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to extract specific number from string starts with "18"
    By rajuuuuu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-06-2018, 08:21 AM
  2. Formula to Extract word after "Number" only
    By oneblondebrow in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-07-2018, 01:46 PM
  3. [SOLVED] Extract Number after the word "Rating:"
    By Nights in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-27-2015, 10:14 AM
  4. Replies: 1
    Last Post: 10-22-2012, 09:38 AM
  5. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  6. Replies: 5
    Last Post: 09-19-2008, 04:02 PM
  7. Replies: 6
    Last Post: 11-01-2007, 11:56 AM

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