+ Reply to Thread
Results 1 to 6 of 6

Delete everything after a certain character

  1. #1
    Registered User
    Join Date
    06-11-2009
    Location
    Swe
    MS-Off Ver
    Excel 2003
    Posts
    27

    Delete everything after a certain character

    Hi,
    I've got some cells that contain names and numbers within parentheses. (the cells located in the A column, starting on cell 2)
    john(5)
    jack(90)
    joe(120)
    I only want to keep the names, so I want to delete the parentheses and everything in them.

    Since the parentheses is always last in a cell, my vba function should find the first parenthesis, and then delete it and everything that comes afterwards.

    I've figured out that i can use 'InStr' to check if the cell contains a parenthesis.
    But I need some help with the deleting part.
    Public Sub DelEverythingAfterParenthesis()
    Dim rngCell As Range
    Set ws = Workbooks("a.xls").Sheets("sheet1")
    For Each rngCell In ws.Range(Cells(2, "A"), ws.Cells(Rows.Count, "A").End(xlUp)).Cells
    If (InStr(rngCell, "(") > 0) Then

    //Some code to delete the parentheses and everything after it.

    End If
    Next rngCell
    End Sub
    Last edited by dadel; 08-11-2009 at 06:03 AM.

  2. #2
    Forum Contributor vikas.bhandari's Avatar
    Join Date
    04-07-2006
    Location
    Delhi, India
    MS-Off Ver
    Office 2007 and 2010
    Posts
    303

    Re: Delete everything after a certain character

    You dont have to write a macro for this.

    Write this formula in C2 and drag it down to the number of rows you want to apply to :

    =LEFT(B2,FIND("(",B2,1) - 1)

    HTH.

    Thanks,
    Vikas

  3. #3
    Registered User
    Join Date
    06-11-2009
    Location
    Swe
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Delete everything after a certain character

    Thank you!
    that did the trick. (changed b2 to a2 though)

    But a wierd problem - while that worked in a test document, I can't get the same forumula working in the document i'm working on!
    The cell just displays the forumla, it doesn't calculate the result, even if I press enter.

    The cells in both documents look the same, have the same format (text), and the same values.
    could there be any problems with the settings or something like that?
    Last edited by dadel; 08-11-2009 at 07:55 AM.

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

    Re: Delete everything after a certain character

    Highlight the column containing the formula then

    ALT + D -> E -> click Finish

    If you format cells to Text prior to entering the formula then the formula will be viewed as a Text string and not a formula, the above corrects that issue.

  5. #5
    Registered User
    Join Date
    07-19-2009
    Location
    toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Delete everything after a certain character

    I have a similar question:

    how can i delete Everything before a certain character ( for example @ ), no matter how much there is before @

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Delete everything after a certain character

    Hello mthem2003,

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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