+ Reply to Thread
Results 1 to 12 of 12

Separate text from numbers

Hybrid View

  1. #1
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Separate text from numbers

    how many are you likely to need?
    here is a (not very efficient udf )that will extract all numbers into one cell .then copy paste special values then split with text to columns
    use as =remove_text(a1) and drag down

    Function Remove_text(Rng As String) As String
    Dim Tmp As String
    Dim i As Integer
    Dim x As Integer
    
    
    Tmp = Rng
    For i = 1 To 47
    Tmp = Application.Substitute(Tmp, Chr(i), " ")
    Next i
    For x = 58 To 255
    Tmp = Application.Substitute(Tmp, Chr(x), " ")
    
    Next x
    Tmp = Application.Trim(Tmp)
    
    Remove_text = Tmp
    End Function
    Last edited by martindwilson; 02-10-2010 at 11:12 AM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  2. #2
    Forum Contributor
    Join Date
    09-19-2007
    Location
    Beirut
    MS-Off Ver
    0365 MSO Version 2109
    Posts
    207

    Re: Separate text from numbers

    Thanks Martin
    the latest solved my problem (almost)
    thanks for your time

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Separate text from numbers

    FWIW - if you opted for UDF - Regular Expressions may be worth considering...

    Function NumberExtract(rngString As Range, Optional lngInstance As Long = 1) As Variant
    Dim RegExp As Object, RegExpMatch As Object
    On Error Resume Next
    Set RegExp = CreateObject("vbscript.regexp")
    With RegExp
        .Global = True
        .IgnoreCase = True
        .Pattern = "[0-9]+"
    End With
    Set RegExpMatch = RegExp.Execute(rngString)
    If lngInstance > RegExpMatch.Count Then
        NumberExtract = ""
    Else
        NumberExtract = RegExpMatch(lngInstance - 1)
    End If
    Set RegExpMatch = Nothing
    Set RegExp = Nothing
    End Function

    Assume string in A1, numbers to go in B1, C1 etc...

    B1: =NUMBEREXTRACT($A1,COLUMNS($B1:B1))
    copied across

    The second variable is optional - ie should you only be looking to return a single numeric value (first) it could be omitted - in this case we increment the value to return 1st, 2nd, 3rd etc...

    Not very effiicient though doing this with functions though IMO...a sub routine would be better.

    EDIT:

    if you want the output for finds to be numeric adjust the 2nd NumberExtract line to:

    NumberExtract = Val(RegExpMatch(lngInstance - 1))
    Last edited by DonkeyOte; 02-11-2010 at 09:18 AM.

+ 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