Consulting

Results 1 to 10 of 10

Thread: Solved: removing part of string

  1. #1

    Solved: removing part of string

    hy,
    have data in column A , and need to remove part of string starting empty space following with number ...

    something like this ,

    replace" num*" , with ""
    example:

    bla bla 224555 --result--> bla bla
    Last edited by thmh; 09-07-2011 at 10:05 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try

    =TRIM(SUBSTITUTE(A2,LOOKUP(9.99999999999999E+307,--MID(A2,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A2&"0123456789")),ROW(INDIRECT("1:" &LEN(A2))))),""))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Another way

    =LEFT(A2,FIND(CHAR(1),SUBSTITUTE(A2," ",CHAR(1),LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))-1)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    formula is good but i need vba code

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub Test_RemoveTrailingNumbers()
    Dim cell As Range
    For Each cell In Range("A1", Range("A" & Rows.Count).End(xlUp))
    cell.Value2 = RemoveTrailingNumbers(cell.Value2)
    Next cell
    End Sub

    Function RemoveTrailingNumbers(aString As String) As String
    Dim a() As String, s As String
    a = Split(aString, " ")
    Debug.Print a(UBound(a)), IsNumeric(a(UBound(a)))
    If IsNumeric(a(UBound(a))) Then
    ReDim Preserve a(0 To (UBound(a) - 1))
    s = Join(a, " ")
    Else
    s = aString
    End If
    RemoveTrailingNumbers = s
    End Function[/VBA]

  6. #6
    VBAX Regular sdruley's Avatar
    Joined
    Sep 2011
    Location
    Conover, NC
    Posts
    6
    Location

    Smile No Number

    See attached example. The numbers can be anywhere in the string.
    Attached Files Attached Files

  7. #7
    Kenneth yore code gave me error see picture,
    punting sdruley function and your sub together worked fine,

    tnx

    here is code
    [vba]

    Function NoNumber(S As String) As String
    Dim L, L1, Str
    Str = S
    L = Len(Str)
    For p = L To 2 Step -1
    n = IsNumeric(Mid(Str, p - 1, 1))
    If n Then
    x = x + 1
    A = Left(Left(Str, p - 1), Len(Left(Str, p - 1)) - 1)
    'B = Right(Str, L - p - x + 1)
    Str = A '& B
    End If
    Next p
    NoNumber = Str
    End Function

    Sub Test_NoNumber()
    Dim cell As Range
    For Each cell In Range("A1", Range("A" & Rows.Count).End(xlUp))
    cell.Value2 = NoNumber(cell.Value2)
    Next cell
    End Sub

    [/vba]



    Last edited by thmh; 09-08-2011 at 08:33 AM.

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Can you post the file where my function failed and say which cells to test?

    With the string that you posted it worked fine. Obviously, Debug.Print is not needed. It would fail later if that line fails though.

  9. #9
    sdruley attached file with some examples , just copy examples in column A , if you remove debug line next line will error

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I could not reproduce an error per se. The last result showed #VALUE because the string was only a number. That case can be coded for.

    My function varied from NoNumber() because I read the request to trim a string with a trailing space character and a number.

    IF the goal was to strip all numbers from a string, then I would have solved the problem in another way.

    The "skjfslkj 23 545 " contained a trailing space character so my UDF did not trim it.

    "2400987234" contains no space character so my UDF did not change it but did show #VALUE which is easily coded for as I said.

    This one should replace all number strings if that was your goal.
    [vba]Function RemoveWordNumbers(aString As String) As String
    Dim a() As String, s As String, i As Integer
    a = Split(aString, " ")
    For i = 0 To UBound(a)
    If IsNumeric(a(i)) Then a(i) = ""
    Next i
    RemoveWordNumbers = Trim(Join(a, " "))
    End Function[/vba]

Posting Permissions

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