+ Reply to Thread
Results 1 to 4 of 4

Clean up code for conditional deletion of columns

  1. #1
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166

    Clean up code for conditional deletion of columns

    Hi All,

    I have the following code that I'm thinking can be made cleaner and more efficient, but I'm not quite sure how:

    Please Login or Register  to view this content.
    Is there a way to rework this so that each cell doesn't actually have to be selected? I'm guessing that this might slow the macro down.

    Many thanks in advance,
    Jeff
    Last edited by JChandler22; 03-19-2010 at 10:50 AM.

  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: Clean up code for conditional deletion of columns

    Hi Jeff,

    Rather than a long loop that examines every cell, a much more efficient way quicker way is to simply filter the data to show all rows that are not equal to contractor, then select and delete all those filtered rows.

    HTH
    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
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Clean up code for conditional deletion of columns

    Your original code doesn't even work correctly as designed. If there are two rows together that both have "contractor" in column D, the second one won't be deleted.

    Indeed, using the autofilter to do it all at once is the way to go.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166

    Re: Clean up code for conditional deletion of columns

    Awesome - thank you both. I use Excel's auto filter feature religoiusly (ctrl + shift + L is one of my first commands when opening any spreadsheet).

    The snippet of code above is part of a larger macro, and that macro will be used by several of my colleagues who are less than tech-savvy. I want to have a magic button for them to push rather than teaching them about auto filters (among other things). What I hadn't considered - and thanks for showing me the light - is that I can integrate the auto filter feature into the macro instead of using the long loop. This will be much more efficient.

    Thanks again.

+ 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