+ Reply to Thread
Results 1 to 10 of 10

Clear cells with specific text in them

  1. #1
    Forum Contributor
    Join Date
    04-02-2013
    Location
    West Palm beach, Fl
    MS-Off Ver
    Excel 2010
    Posts
    113

    Clear cells with specific text in them

    I'm still in the early learning stages of VBA. But, I have a column that has somewhat random text in it. I a trying to make it so that it clears the content from all cells that don’t contain a certain text string.
    Ex: Clear every cell that doesn’t have the words “Company Code: “. So if B123 = "Company Code" it would NOT erase it, but if it says “Department” it will because it doesn’t contain that specified text string.
    I am thinking that a simple “If, then” type function would work. If it contains the text string do nothing otherwise clear the content type of deal. I'm stuck though. Any suggestions? Here is what I got so far:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Clear cells with specific text in them

    Please Login or Register  to view this content.
    Last edited by stnkynts; 01-26-2015 at 11:51 AM.

  3. #3
    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: Clear cells with specific text in them

    This question really should be a FAQ. I see it time and time again and looping just isn't the best way of doing this sort of thing.

    Whenever you find yourself creating a loop step back a bit and consider if there isn't a more efficient way. Loops should only be used as a last resort since they can be very slow particularly when many rows are involved.

    The fastest and most efficient way I know of doing this is to use an autofilter and filter for whatever is the relevant criteria. Then select all the filtered rows using the .SpecialCells(xlCellTypeVisible) construct, delete them all, finally remove the autofilter.

    In your case, untested in the absence of the workbook but try

    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.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Clear cells with specific text in them

    FWIW:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Clear cells with specific text in them

    @Richard. What you said is 100% true. I would like to point out that the OP said "every cell that doesn't have the words..." which indicated to me that the string could contain more than "Company Code". In this case I don't think Autofilter would work. Technically the most efficient would probably be to use a dictionary/expression, but, since the OP supplied code it might be better to return similar code back that he feels more comfortable working with. Once again, you are absolutely correct about the use of loops.

    Just my newbie 2 cents.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Clear cells with specific text in them

    Autofilter works. For does not contain. - Criteria1:="<>*Company Code:*"

  7. #7
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Clear cells with specific text in them

    Ahh thanks John. Learned something new to put away in the toolbox

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Clear cells with specific text in them

    You're welcome, stnkynts. Richard is right by the way. His way is much more efficient. Guess we all get stuck in the Loop habit.

  9. #9
    Forum Contributor
    Join Date
    04-02-2013
    Location
    West Palm beach, Fl
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: Clear cells with specific text in them

    Hey everyone, thank you for the advice! I will have to rethink some things. The code works, on all three accounts. Thanks for the help and knowledge!

  10. #10
    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: Clear cells with specific text in them

    Hi,

    @stnkynts
    Fair point and I accept that if the OP meant (and re-reading his original I think you're correct) that the column contained all sorts of labels and blanks and only blanks should be deleted then as stated it would delete all apart from Company Code. In which case the filter line might have been better as
    Please Login or Register  to view this content.
    but having seen John's remark about 'contains' he's reminded me of something that I have a vague recollection of reading before, so from me too - thanks John.

    As for working with and editing code that's given: It's a moot point. I agree that should be the default position but when I see code that is badly written or redundant, or where there are lots of .Selects .Activates and other stuff that is clearly from the macro recorder I believe it's important to not only indicate there are more efficient ways but also show how. The looping thing to determine whether to process a particular row is a particular bete noir of mine and something that impressed me very early on when I was in the corporate world and handled some large tables. I used to waste a lot of time waiting for macros to complete until someone mentioned the filtering method which enables whole chunks of data to be processed immediately. My life was transformed and I've never forgotten it. If we were ever asked for a top tip that would be high on my list.

+ 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] code to clear all contents in columns with specific text in row 2
    By xcelnovice101 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-30-2014, 08:58 AM
  2. [SOLVED] Clear contents of specific cells on the same row if a specific cell on that row is empty
    By djfscouse in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2014, 01:48 PM
  3. [SOLVED] VBA Macro To Clear Cell When Specific Text Is Entered
    By paulm99 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-14-2014, 06:52 PM
  4. [SOLVED] Clear specific cells
    By ecelaras in forum Excel General
    Replies: 5
    Last Post: 03-10-2013, 05:06 PM
  5. Clear contents of cells that do not contain specific text, sort cells that do
    By feckless.lout in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-27-2010, 01:41 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