+ Reply to Thread
Results 1 to 12 of 12

Macro to remove rows that don't contain a specific string in column B

  1. #1
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Macro to remove rows that don't contain a specific string in column B

    Hi,

    I'm sure this is a commonly asked question, but I'd like this variant specifically for column B.

    e.g.

    If column B does not specifically contain "ART" (not case sensitive), then the entire row should be deleted.
    Not left blank, but deleted.


    All help appreciated

    Thanks!

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to remove rows that don't contain a specific string in column B

    Hi,

    One way

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro to remove rows that don't contain a specific string in column B

    When I tried it on my workbook, I got "Can't complete requested operation" ?

  4. #4
    Registered User
    Join Date
    09-02-2013
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Macro to remove rows that don't contain a specific string in column B

    works okish here but will not delete row 1

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to remove rows that don't contain a specific string in column B

    Quote Originally Posted by k1w1sm View Post
    works okish here but will not delete row 1
    I had assumed that row 1 would typically contain a column description header that you wouldn't want to delete.

    Just remove the

    .Offset(1, 0)
    in the line
    .Offset(1,0).SpecialCells(xlCellTypeVisible).Rows.Delete
    to leave
    .SpecialCells(xlCellTypeVisible).Rows.Delete

  6. #6
    Registered User
    Join Date
    09-02-2013
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Macro to remove rows that don't contain a specific string in column B

    Quote Originally Posted by Richard Buttrey View Post
    I had assumed that row 1 would typically contain a column description header that you wouldn't want to delete.

    Just remove the

    .Offset(1, 0)
    in the line
    .Offset(1,0).SpecialCells(xlCellTypeVisible).Rows.Delete
    to leave
    .SpecialCells(xlCellTypeVisible).Rows.Delete
    Not a problem. It was a nice solution, once I stepped through it and realised how it worked, I would have written a macro to loop through the lines. But I like yours better.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to remove rows that don't contain a specific string in column B

    As a general rule whenever you find yourself writing some loop code take a step back and reconsider. Loops, especially with large ranges have a big time overhead since every pass through the loop means jumping from VBA to the Excel App and then a return to VBA.

    Often you can use an Autofilter - sometimes you may need to use a helper column with a formula that works out which rows should be identified for hiding, deleting, or copying, and then use a single line of code to address the whole block of filtered rows.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to remove rows that don't contain a specific string in column B


    Hi !

    yes for big data I prefer to sort the data with a helper column and clear the block of rows to delete at once,
    faster than deleting one by one …

  9. #9
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro to remove rows that don't contain a specific string in column B

    Hi,

    I just realized that it's working (though I still get the message)
    Is there any way that the rows I delete can apply to all sheets?

    For example, if on sheet 1 (which the macro is run on) rows A B C are deleted, then rows A B C on all sheets are deleted?

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to remove rows that don't contain a specific string in column B

    Maybe this modification to Richard's code:

    Please Login or Register  to view this content.
    *But I don't know a way to guarantee that they are the same rows
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  11. #11
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro to remove rows that don't contain a specific string in column B

    Well...I tried it and I got "cannot be applied to selected range" and it wiped out all other data except the first row in the other worksheets.

    So that didn't work...

    Is there a way to somehow record which rows are to be deleted then apply that to the other sheets?

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to remove rows that don't contain a specific string in column B

    This runs:

    Please Login or Register  to view this content.

+ 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. Replies: 16
    Last Post: 04-10-2016, 07:51 AM
  2. Looking for macro to remove rows with multiple text string values
    By shufflemo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2016, 03:29 PM
  3. Need help with a macro to add & remove rows in specific locations
    By thesteve in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2015, 11:06 AM
  4. Macro to remove date string from specific file names
    By wouterd0 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-12-2014, 11:37 AM
  5. excel macro to remove specific columns and rows + remove duplicate
    By garrywelson in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-17-2013, 12:03 PM
  6. Modify to Run On Specific Column and Remove trailing digits in string
    By pmterp in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-12-2012, 07:47 PM
  7. vba macro to remove rows if different from specific cell
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-06-2011, 01:28 PM

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