+ Reply to Thread
Results 1 to 3 of 3

Formula to separate numbers from text

Hybrid View

  1. #1
    Registered User
    Join Date
    01-20-2009
    Location
    Toulouse, France
    MS-Off Ver
    Excel 2007
    Posts
    1

    Formula to separate numbers from text

    Hello,

    I can't find which formula to use in order to separate numbers from text in the following column:

    60g
    42.5g
    227g
    355ml
    2L

    Any thoughts? You help would be deeply appreciated!

    Anne-Claire

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531
    there will be other ways, no doubt more elegant...

    =--LEFT(A1,SUMPRODUCT(--(ISNUMBER(--MID(SUBSTITUTE(SUBSTITUTE(A1,".",0),",",0),ROW(INDIRECT("1:"&LEN(A1))),1)))))

    where A1 holds string... note in the above I've had to make the assumption that "." & "," are valid numerical delimiters (eg 42.5 or 1,002 etc...)

    If it's possible that you would have additional numbers beyond the value of interest (eg 42.5g 10a) you would need to adopt a different approach (where you look for the first text character)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    A user-defined function can do it. Insert this new function into your sheet in the same place regular macros go:
    Function CleanAll(txt As String) As String
    With CreateObject("VBScript.RegExp")
    'enter all characters to be KEPT into the pattern
        .Pattern = "[^0-9.]"
        .Global = True
        .IgnoreCase = True
        CleanAll = Application.Trim(.Replace(txt, ""))
    End With
    End Function
    Now this formula will leave only numbers and periods:

    =CleanAll(A1)

    ==========
    Alt-F11 to open VBEditor
    Insert > Module
    Paste in code
    Alt-Q to exit VBEditor
    Save sheet.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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