+ Reply to Thread
Results 1 to 12 of 12

Excel 2007 : Code to delete text after comma

  1. #1
    Registered User
    Join Date
    05-13-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    78

    Code to delete text after comma

    Is there any way to write a macro that will delete the text after ",".

    The data is in below form:

    1) Boston University, Boston College Law School
    2) Harvard University, Columbia University School of Law

    I am thinking of macro that will delete the text after "," like:

    1) Boston University
    2) Harvard University

    Any help would be highly appreciated.

    Thanks.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Code to delete text after comma

    Would a formula not be enough?
    Please Login or Register  to view this content.

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Registered User
    Join Date
    05-13-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Code to delete text after comma

    Thanks Marco!

    You are correct, but there are more than 20K rows and I have to apply this on filtered rows that's why I am looking for a macro.

  4. #4
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Code to delete text after comma

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Code to delete text after comma

    So why not remove the filter(s), this will also reveal manually hidden rows, apply the formula, then re-apply the filter(s)?

  6. #6
    Registered User
    Join Date
    05-13-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Code to delete text after comma

    Please find attached sample workbook along with the requirements inside.

    Thanks.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-13-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Code to delete text after comma

    Finally, I figure out the solution:

    Code:
    Dim s As String
    Sub removeco()
    With WorksheetFunction

    Set a = Range("a1:a26")

    For Each a In Range("a1:a26")

    If Len(a) - Len(.Substitute(a, ",", "")) < 2 Then

    s = Left(a.Value, .Find(",", a.Value) - 1)
    a.Value = s
    End If
    Next a
    End With
    End Sub
    [\CODE]

  8. #8
    Registered User
    Join Date
    05-13-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Code to delete text after comma

    Finally, I figure out the solution:

    Please Login or Register  to view this content.

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Code to delete text after comma

    Try this in C2 (without filters)
    =IF(LEN(A2)-LEN(SUBSTITUTE(A2,",",""))=1,LEFT(A2,FIND(",",A2)-1),"")
    Drag/Fill Down.

    Hope this helps.

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Code to delete text after comma

    Better with this rather than a loop for 20k plus rows
    Please Login or Register  to view this content.

    Hope this helps.

    [EDIT]
    P.S.

    Don't Dimension your variables outside the Sub that they apply to unless you need to carry them over several Subs, even then be careful how and when you call them.
    Last edited by Marcol; 07-22-2011 at 07:11 AM.

  11. #11
    Registered User
    Join Date
    05-13-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: Code to delete text after comma

    Thanks Marcol! It works

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Code to delete text after comma

    Happy to have helped.

+ 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