+ Reply to Thread
Results 1 to 9 of 9

Remove all text after the third ocurance of a special character

  1. #1
    Registered User
    Join Date
    06-07-2012
    Location
    US
    MS-Off Ver
    Excel Office 365
    Posts
    56

    Remove all text after the third ocurance of a special character

    I have a bunch of data in a column. The data can have dashes but never more that 2. Sometimes I get 3 dashes and need to remove the third dash along with everything after it. Is there a way for a piece of VBA code can check if the cell has 3 dashes and if does, remove the third dash along with everything after it? If the cell doesn't have 3 dashes do nothing. Then continue to the end of the data.

    apple -> apple
    pear-01 -> pear-01
    peach-01-23 -> peach-01-23
    banana-02A-3F-01 -> banana-02A-3F

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove all text after the third ocurance of a special character

    nguerra,

    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.

    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

    To attach your workbook, scroll down and click on the Go Advanced button, then scroll down and click on the Manage Attachments button.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Remove all text after the third ocurance of a special character

    Hi nguerra,

    Agree with Stan, but here's my attempt (which if it doesn't work, your actual data will be vital to providing an answer):

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    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: Remove all text after the third ocurance of a special character

    Hello nguerra,

    This code will do it.
    Please Login or Register  to view this content.
    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!)

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Remove all text after the third ocurance of a special character

    Hi Leith,

    Long time no chat. Hope you and yours are well.

    Great code

    Hi nguerra,

    I'd use Leith's code as it's more streamlined and therefore easier to follow than mine.

    Regards,

    Robert

  6. #6
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove all text after the third ocurance of a special character

    nguerra,

    If your raw data strings are in column A, beginning in cell A1, down, then:


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    1. Copy the below code
    2. Open your NEW workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

    Please Login or Register  to view this content.
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the ReorgData macro.

  7. #7
    Registered User
    Join Date
    06-07-2012
    Location
    US
    MS-Off Ver
    Excel Office 365
    Posts
    56

    Re: Remove all text after the third ocurance of a special character

    First of all I would like to apologize to everyone who helped me. After posting this question I lost my internet connection and didn't get it back until today. I was able to get both Trebor76 and stanleydgromjr to work on my worksheet. Both of them ran perfect. I suspect that if I had attached a sample workbook, Leith's code would have worked as well. Thank's to all three of you who took time to help me out.

  8. #8
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove all text after the third ocurance of a special character

    nguerra,

    Thanks for the feedback.

    You are very welcome. Glad we could help.

    And, come back anytime.

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Remove all text after the third ocurance of a special character

    Yes you are very welcome. If you could mark the thread as solved it would be appreciated.

    Regards,

    Robert

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. remove a special character using VBA
    By phwjr in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-06-2013, 02:33 PM
  2. how to remove the * special character in excel
    By mohan_srijaya in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-14-2013, 06:26 PM
  3. Macros to remove a special character and all the characters after that....
    By Umapathy kumar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-07-2013, 01:27 AM
  4. Replies: 2
    Last Post: 10-24-2011, 07:54 AM
  5. remove special character at end of string
    By captain bob in forum Excel General
    Replies: 0
    Last Post: 08-03-2006, 09:59 AM

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