+ Reply to Thread
Results 1 to 9 of 9

BEST way to delete rows based on cell value

  1. #1
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    BEST way to delete rows based on cell value

    I am using the following code to work backwards though a range, deleting rows if the cell does not contain a value beginning with "J"

    In the past I have used several other ways to delete rows in a range, so I'm wondering... what the best way to do it?

    Please Login or Register  to view this content.
    My range consists of having a parent row followed by 6 child rows, and it's these child rows that are being deleted.
    So would a better way be to block-delete the rows by just selecting/activating the entire range, then deselecting the 1st and every 7th row?

  2. #2
    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: BEST way to delete rows based on cell value

    Hello Echo Passenger,

    Did you want the rows to shift up after they are deleted or can they remain blank?
    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!)

  3. #3
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    Re: BEST way to delete rows based on cell value

    Yes to delete and shift up the subsequent rows.

    Although I have asked about the 'best' way in the OP, I am a big believer in brevity of code, so I would like to keep it as simple as possible.
    In that regard, I have been playing around with a few alterations and currently the fastest (although the time to run differences are very minimal) version seems to be rather than test each cell for a value <> "J*", just delete an offset range of the below 6 rows then move down the next parent row (which has now) shifted up to the next row) and repeat.


    Please Login or Register  to view this content.

    Looking back at old code, I have used a different, far more verbose, loop as well as the AutoFilter to delete rows. I think what I have now is probably the best approach.

    I am still curious though, could I select my entire range A3:A* then just deselect the first and every 7th row, then delete the remaining selection in one go? Is there even a 'remove from selection' command?
    Last edited by EchoPassenger; 01-20-2017 at 06:38 PM.

  4. #4
    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: BEST way to delete rows based on cell value

    Hello EchoPassenger,


    This will delete the lines in blocks of 6. This assumes there are no blanks between the "parent" and it "child" lines.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    Re: BEST way to delete rows based on cell value

    Looks good, I'll incorporate this.

    Is there any benefit to using Cells(Rows.Count, "A").End(xlUp).Row over say Range("A65536").End(xlUp).Row? Aside from not having to enter a range start?

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: BEST way to delete rows based on cell value

    This is a non-looping approach

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: BEST way to delete rows based on cell value

    Quote Originally Posted by EchoPassenger View Post
    Looks good, I'll incorporate this.

    Is there any benefit to using Cells(Rows.Count, "A").End(xlUp).Row over say Range("A65536").End(xlUp).Row? Aside from not having to enter a range start?

    not all worksheets have 65536 rows.
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  8. #8
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    Re: BEST way to delete rows based on cell value

    Quote Originally Posted by bulina2k View Post
    not all worksheets have 65536 rows.
    I was asking more with regards to the general technique rather than the specifics of that line.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: BEST way to delete rows based on cell value

    Quote Originally Posted by EchoPassenger View Post
    ...
    Is there any benefit to using Cells(Rows.Count, "A").End(xlUp).Row over say Range("A65536").End(xlUp).Row? Aside from not having to enter a range start?
    In all versions of Excel that support VBA, every worksheet has 65536 (or more) rows.
    However, the more recent versions have many more and might have more than 65536 rows of data.

    If you are 100% sure that there will be nothing below row 65535, then there is no difference, but the Rows.Count approach is more certain.

+ 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] delete rows based on cell value
    By shakir1401 in forum Excel - New Users/Basics
    Replies: 9
    Last Post: 12-21-2015, 10:22 AM
  2. Macro to delete rows below based on cell value
    By dobracik in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-27-2015, 03:53 PM
  3. Delete rows based on the value of a cell
    By Emma_B in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-04-2013, 05:35 PM
  4. Delete certain rows based on value in another cell
    By angelk in forum Excel General
    Replies: 1
    Last Post: 07-30-2012, 02:38 PM
  5. Delete Rows based on cell value and then Insert rows alternatively with a formula
    By acsishere in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-11-2008, 04:27 PM
  6. Delete rows based on a cell value in the row
    By Dave in forum Excel General
    Replies: 7
    Last Post: 03-14-2006, 02:50 PM
  7. Delete rows based on Cell name
    By gmunro in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2005, 09:15 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