+ Reply to Thread
Results 1 to 11 of 11

VBA Code to delete Row on Content of one Cell

  1. #1
    Registered User
    Join Date
    02-27-2014
    Location
    Nairobi, Kenya
    MS-Off Ver
    Excel 2010
    Posts
    7

    VBA Code to delete Row on Content of one Cell

    Hi All

    Trying to learn VBA on the fly as I go. Have a relatively simple task that I thought I had sussed, but apparently not. I have a data range A:O that is collected from other sheets (using a Master and Report Sheet) and pasted into one sheet as values only not the formulas in the Report sheet.

    Because it collects a standard length this means there are gaps. In the report sheet I have the formula that states If(ISNOTTEXT) then True returns value Don't Delete and False is Delete. When this is copied into the new sheet as values only the column O has either Delete or Don't Delete. There can be a lot of values in this one sheet (10,000 mostly blank). If I click the row O, go to find, Delete, select all, right click, delete entire row, I get the effect I want. 178 - 200 rows for Columns A:O. However, I wanted to make a button that would do this for me.

    This was the code I came up with

    Please Login or Register  to view this content.
    However, when I click the button my excel goes into a mad frenzy of calculating and processing and eventually crashes. Is it because I have too many entries or am I not being specific enough? It only needs to select all the cells in column O that contain the word Delete only and then delete entire row.

    Thanks so much

    Jes

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: VBA Code to delete Row on Content of one Cell

    Try like this

    Please Login or Register  to view this content.
    Kind regards
    Leo

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Code to delete Row on Content of one Cell

    You need to loop backwards if you want to delete rows, or use a filter which is easier and faster than looping.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: VBA Code to delete Row on Content of one Cell

    Hi Jes1397

    As AM 33 said, a very important point when you do anything in a “progression” . or Forward looping or “going upwards” ...... You can always get caught out when deleting things.
    . You can experiment to death with real code situations, but in simple plain English:

    . When I loop with a +ve step or “move forwards” or “go upwards” and then delete something, the next thing I wanted to consider slips down into the “hole” left by the deletion. So when I “move” on I go onto the what is actually the “next next “ and miss out what should have been the next as this thing has slipped into the “hole” at my present position where i deleted.

    . Similarly “going forward” and deleting can cause nasty VBA errors, possibly as you have been experiencing, ias VBA may look for the thing to consider in a for each or similar loop type situation.
    . This can occur when VBA tries to consider something it thinks should be there as it has not considered it yet. ( VBA sometimes makes an internal memory at the start of a Loop of all things and where they are, and , at least partially, ignores attempts by you to change variable associated with the Loop Control. If then in the Loop you purposely or unintentionally change variables, by, for example, "going out of step” in a forward Loop, you can confuse VBA if it looks for something being sure it should be there but it isn’t ( anymore ) !!

    . Going or Looping “backwards” means the bits that are “behind me going upwards” slip down and fill in the hole I just made. All these bits have been considered already. The next thing i then consider going backwards is the true next thing i wanted to consider.
    .
    . Hope that somewhat layman explanation helps
    Last edited by Doc.AElstein; 09-09-2015 at 05:45 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

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

    Re: VBA Code to delete Row on Content of one Cell

    Alan,
    You quite often write long sentences, but this simple jargon-free explanation caught my eyes. You have done a great job.

  6. #6
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: VBA Code to delete Row on Content of one Cell

    Hi
    Quote Originally Posted by AB33 View Post
    Alan,
    You quite often write long sentences, but this simple jargon-free explanation caught my eyes. ......
    Thanks...
    ironic – the simplest are often the best. I was feeling a bit too tired to write my usual “book”... with examples..
    Alan.

  7. #7
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: VBA Code to delete Row on Content of one Cell

    Jes,

    Consider using AutoFilter instead of looping by cell. Once you filter the Range you can delete the entire visible Range in one go.

    Please Login or Register  to view this content.
    Kind regards,
    w

    http://dataprose.org

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

    Re: VBA Code to delete Row on Content of one Cell

    Please Login or Register  to view this content.
    I think he needs column O. Good catch on the autofilter though.
    If you are happy with my response please click the * in the lower left of my post.

  9. #9
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: VBA Code to delete Row on Content of one Cell

    Thanks stnkynts,

    Change Field:=2 to the correct Field number. If "O", then Field:=15

    Even better would be to use an InputBox to prompt the user for some Text, search for the Text, and return the Field the Field Number. This would make the code more dynamic.
    Same goes for Criteria instead of hard-coding a value.

  10. #10
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: VBA Code to delete Row on Content of one Cell

    Thanks stnkynts,

    Change Field:=2 to the correct Field number. If "O", then Field:=15

    Even better would be to use an InputBox to prompt the user for some Text, search for the Text, and return the Field the Field Number. This would make the code more dynamic.
    Same goes for Criteria instead of hard-coding a value.

  11. #11
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA Code to delete Row on Content of one Cell

    You have already created a range rng from the current region.
    You don't need to create another range and you don't need special cells.
    Since you created rng from the current region, this is all you need.

    Please Login or Register  to view this content.
    Last edited by skywriter; 09-08-2015 at 06:50 PM.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

+ 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. VBA Code Delete Cell Content if met criteria " "
    By TNT Innight in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-24-2014, 07:15 AM
  2. [SOLVED] Need help with VBA code to perform a calculation, replace cell content and delete row
    By Ellen 2Excel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-26-2013, 07:13 PM
  3. IF same CONTENT FOUND IN CELL A THEN DELETE CONTENT IN B,C or D
    By mecutemecute in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-11-2013, 01:16 AM
  4. [SOLVED] delete cell content when other cell's content is removed
    By Pretpik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2012, 11:06 PM
  5. Delete Cell Content
    By jamer02 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2009, 08:47 AM
  6. what code can delete cell content
    By colwyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2008, 08:19 AM
  7. [SOLVED] Delete cell content
    By Chuck Neal in forum Excel General
    Replies: 3
    Last Post: 06-14-2006, 12:10 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