Consulting

Results 1 to 13 of 13

Thread: Solved: truncate string and onward

  1. #1

    Solved: truncate string and onward

    I'd like to find "this text" and delete that string plus anything that follows it from a selection of cells in excel.

    I did a couple of searches but to no avail.

    thanks

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Try:
    [vba]
    Selection.Replace What:="this text", Replacement:=vbNullString, LookAt:=xlPart, MatchCase:=False
    [/vba]

    Hope that helps,

    Mark

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Sub DoReplace()
    Dim cel As Range
    For Each cel In Selection
    If InStr(1, cel, "this text") > 0 Then
    cel.Value = Left(cel, InStr(1, cel, "this text") - 1)
    End If
    Next
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Quote Originally Posted by GTO
    Try:
    [vba]
    Selection.Replace What:="this text", Replacement:=vbNullString, LookAt:=xlPart, MatchCase:=False
    [/vba]

    Hope that helps,

    Mark

    This actually finds and erases "this text" but everything after it remains.

  5. #5
    Quote Originally Posted by mdmackillop
    [vba]
    Sub DoReplace()
    Dim cel As Range
    For Each cel In Selection
    If InStr(1, cel, "this text") > 0 Then
    cel.Value = Left(cel, InStr(1, cel, "this text") - 1)
    End If
    Next
    End Sub
    [/vba]
    I wonder if a parentheses is missing here on the cel.value line since it didn't work.

  6. #6
    Hi mikeo1313,

    Assuming there's a space after "this text" in the cell i.e. "this text " (remove the extra space from the following if not) try:

    Sub DoReplace()
        
        Dim cel As Range
        
        For Each cel In Selection
            If InStr(1, cel, "this text") > 0 Then
                cel.Value = Replace(cel, "this text ", vbNullString)
            End If
        Next
        
    End Sub
    HTH

    Robert

  7. #7
    To clarify here is an example of desired output:

    sample :
    vba soup, this text must go plus any random characters that follow from selection of cells

    result :
    vba soup,

  8. #8
    visual basic code from this link supposedly does it, here is a link JIK it helps though I guess vba & VB are totally different.

    http://www.freevbcode.com/showcode.asp?id=2832

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Small change to Mark's suggestion: the asterisk after the search text


    [VBA]
    Selection.Replace What:="this text*", Replacement:=vbNullString, LookAt:=xlPart, MatchCase:=False
    [/VBA]

    Paul

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Oopsie!

    Thanks Paul :-)

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by mikeo1313
    I wonder if a parentheses is missing here on the cel.value line since it didn't work.
    Try deleting a bracket and see what happens. Can you explain how you implemented the code, as I'm mystified.

    It works for me returning

    vba soup,
    vba soup,
    vba soup,
    and so on
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mikeo1313
    I wonder if a parentheses is missing here on the cel.value line since it didn't work.
    A guess, but by working, if you do not mean it errored, but that it just didn't replace/strip text... might your actual strings be in upper/mixed case?

  13. #13
    Quote Originally Posted by mdmackillop
    Try deleting a bracket and see what happens. Can you explain how you implemented the code, as I'm mystified.

    It works for me returning

    vba soup,
    vba soup,
    vba soup,
    and so on
    I can't explain for the life of me why it worked today and not yesterday, as I just deleted and put the same code today, tried it again and worked.

    Maybe my machine had the vba flu + it wasn't an issue with case either.

    Thank you all, both methods do work greatly

Posting Permissions

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