+ Reply to Thread
Results 1 to 11 of 11

Deleting rows that do not contain a specific character

  1. #1
    Registered User
    Join Date
    07-26-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    47

    Deleting rows that do not contain a specific character

    (I just posted this in a different sub forum, but I think it belongs here. Not sure how to delete the other one...)

    Saying I'm a beginner to VBA would be an overstatement. I'm a beginner in that I just now installed it for Excel 2007 for the first time. I registered an account here in order to view this thread: Deleting rows based on conditional formatting. It helped a little.

    At work, I've been tasked with figuring out how to delete all rows in a spreadsheet which are not flagged as invalid data. When imported to Excel from the .csv file, there is a column containing the flags. I want to delete all rows of data which do not contain a "C" flag in the final column. Since I will need to do this for multiple workbooks containing tens of thousands of rows each, I realized it's time to learn how to use VBA rather than doing this manually.

    I used the conditional formatting feature to highlight all cells in the flag column containing "C". I'm sure this can probably be done without conditional formatting, too.

    Baby steps.

    I have no background knowledge in any sort of coding. I've been reading a Visual Basic tutorial, but it's still sort of Greek to me.

    I've attached a test file. I need to delete all rows that do not contain the letter C in column J.

    From the tutorial, I have:

    Please Login or Register  to view this content.
    Where I have “B<” I need a way to specify to delete all rows not containing C, or all rows which do not have a highlighted cell in the test file.

    Any help will be greatly appreciated! Thank you in advance
    Attached Files Attached Files
    Last edited by SaraLynne; 07-26-2013 at 01:09 PM.

  2. #2
    Forum Guru
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,445

    Re: Deleting rows that do not contain a specific character

    I am not sure if you want to delete the cells with colours or the other way round.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    07-26-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Deleting rows that do not contain a specific character

    Quote Originally Posted by AB33 View Post
    I am not sure if you want to delete the cells with colours or the other way round.
    That's exactly what I needed! Deleting the rows without color. Thank you so much! This is going to save me so much time I'll have to study up on this stuff, it's very useful.

  4. #4
    Registered User
    Join Date
    07-26-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Deleting rows that do not contain a specific character

    Oh man, it worked beautifully when I ran it in the test file but for some reason in the full data file it deletes all but one row... Hmm, time to investigate! I'll see if I can figure out the cause based on yours. Thanks again!

  5. #5
    Forum Guru
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,445

    Re: Deleting rows that do not contain a specific character

    Can you post a large sample to see what is going?

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    35,968

    Re: Deleting rows that do not contain a specific character

    An alternative approach but not better than using a filter.

    Note that the code should really be in a standard module.

    Regards, TMS

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Excel Aid

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Forum Guru
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,445

    Re: Deleting rows that do not contain a specific character

    Trevor,
    Thanks!
    I did not realise I put in the code on a sheet.
    Last edited by AB33; 07-26-2013 at 12:48 PM.

  8. #8
    Forum Guru
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,445

    Re: Deleting rows that do not contain a specific character

    Sally,
    Please put the code in on standard module and when you run the code, you should run it from the active sheet

  9. #9
    Registered User
    Join Date
    07-26-2013
    Location
    Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Deleting rows that do not contain a specific character

    Wow, it's beautiful! Tested it on the real data sheet and it's perfect. Thank you so much! I'm pretty excited to learn how to use VBA now.


    Maybe I'll even take a few CS elective in my final semester too.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    35,968

    Re: Deleting rows that do not contain a specific character

    SaraLynne

    Thanks for the rep.

    Wow, it's beautiful!
    To what do you refer?

    @AB33: the original code was in a sheet module ... I did the same but thought it worth making the point.

    Regards, TMS

  11. #11
    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: Deleting rows that do not contain a specific character

    Hello SaraLynne,

    Here is a faster and less complex method.
    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!)

+ 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. [SOLVED] VBA: Deleting rows that do not contain a specific character
    By SaraLynne in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-04-2013, 10:29 AM
  2. Deleting rows if specific cell is blank or have specific text
    By JoaoFerreira1985 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-11-2013, 01:18 PM
  3. Replies: 2
    Last Post: 11-09-2011, 11:47 AM
  4. Macro help deleting rows with selected character strings
    By rliccion in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2009, 01:45 PM
  5. Deleting Specific Rows
    By Desi_bhai in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-18-2007, 09:33 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