+ Reply to Thread
Results 1 to 17 of 17

Rows.Delete not deleting first row in range

  1. #1
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Rows.Delete not deleting first row in range

    Hi,

    The following code is working as needed on all rows except the first row in range.
    As you can see its working from last row to row 9, it will delete all rows that match values in 2 columns and text in another.

    This is deleting all rows except row 9 which I thought was included, but row 8 is locked, would that make a difference?

    Where am I going wrong?

    Please Login or Register  to view this content.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    Wouldn't have thought so. Nothing to do with the looped range. You could use Or instead of ElseIf.

    More likely the data isn’t what you think it is. Can't help there without seeing the file.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  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: Rows.Delete not deleting first row in range

    Hi,

    Maybe because the 9th iteration of the loop isn't processed since r is incremented at the last but one NEXT instruction.

    However looping just isn't the best way of doing this sort of thing.

    Whenever you find yourself creating looping code stop and think is this really necessary? Most of the time it isn't. Loops are inherently slow when they have to jump back and forth between VBA and the Excel application - there's a big time overhead at each pass through the loop.

    The fastest, most efficient and elegant way for this sort of stuff is to use an Autofilter to filter the rows you're interested in.
    Please Login or Register  to view this content.
    This uses just one Delete instruction rather than many if you're looping through many cells.
    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
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Rows.Delete not deleting first row in range

    Quibble: AutoFilter requires having a column on which to apply it. You should have been explicit about that.

    Alternatively, construct an ad hoc criteria range for an Advanced Filter, then clear it after deleting rows and removing the filter. Another alternative: use a For loop to create a range object using Union, then delete that range upon completing the loop.

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Rows.Delete not deleting first row in range

    Make use of formula in helper column and delete all matching at once with autofilter...
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  6. #6
    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: Rows.Delete not deleting first row in range

    Quote Originally Posted by hrlngrv View Post
    Quibble: AutoFilter requires having a column on which to apply it. You should have been explicit about that.
    The code I offered did necessarily show the field on which to filter. It would have debugged otherwise.
    When you say being 'explicit' what did you have in mind?

    The essential point was to make the point that loops should be the last resort for stuff like this

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    While there have been several alternative ways offered of achieving the intended goal, that is really not the point. Without seeing the file, it is impossible to guess at the reason that row 9 is not deleted IF, as stated, it meets the criteria.

  8. #8
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Rows.Delete not deleting first row in range

    OP showed code that s/he was using column H to determine bottommost row, which implies there was something already in column H. However, the OP's code indicated that values to determine which rows to delete were in columns G, H and I. Your .CurrentRegion.AutoFilter field:=8, Criteria1:=True doesn't reflect those facts.

    I understood you meant to add another column with formulas like X2: =AND(I2="True ",OR(AND(-5<H2,H2<0),AND(0<I2,I2<5))) and adjust the statement above to Field:=24. However, I'm not the one asking for help.

    I'm sorry I wasn't explicit that your previous response lacked explicit details.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    There is nothing wrong with your code. It is, therefore the data. I suspect that, instead of "True ", you have "TRUE" without the trailing space. Whatever, that cannot be proven without the workbook.

    In the absence of that workbook and, as a sign of frustration, I have created one and tested the code, and some variations. It is attached.

    The code variations:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by TMS; 01-28-2021 at 05:16 PM.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    Updated version incorporating Richard Buttrey's solution, reflecting hrlngrv's comments.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by TMS; 01-28-2021 at 05:24 PM.

  11. #11
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Re: Rows.Delete not deleting first row in range

    First of all my apologies everyone and thanks for the time you have taken, I was at work all day and then other commitments.

    OP states I want row 9 to delete if criteria is met but my code does not delete row 9, I hope that clears up the confusion #7
    #3 confused me as I couldn't see the reference to all 3 columns I was querying but that was picked up on in another post.

    I haven't tried any of your suggestions yet as I wanted to get back to you all.

    I have attached a sample now, columns E & J data removed for reasons and some macros won't work because data has been changed.

    For this exercise just working on 'Filtered' sheet and with 'Button 1' which runs macro 'DeleteFiltered'
    This should delete all columns E:K and shiftup when...
    G: value >0 & <5 with 'Till Difference CASH' in I:
    H: value <0 & >-5 with 'Till Difference CASH' in I:

    The reason for <>0 and not =0 is that it would delete the rows matching in the other column

    This sample achieves this (with looping? which slows it) but row 9 that should get deleted doesn't

    I thought my code should work so not sure where it goes wrong.
    Attached Files Attached Files

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    You have a leading space on the text in row 9, column I (cell I9)

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    Not that it matters, but your code is password protected so we can't see your code.

  14. #14
    Registered User
    Join Date
    11-12-2017
    Location
    Stafford, England
    MS-Off Ver
    365
    Posts
    184

    Re: Rows.Delete not deleting first row in range

    Oh shoot sorry I thought I removed that before saving it, attached is unprotected.

    It seems I do have leading and trailing space on random lines which affect it as I deleted these before copying data and it worked.

    I guess I can fix this with find/replace to format the entire column to last row.

    But as was mentioned earlier this coding causes it to run slow so is there another way to do it.

    But just to clarify if I remove the leading/trailing spaces then t seems to work.

    Thanks everyone cause it seems I've learnt something from coming here
    Attached Files Attached Files

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    Your code tests for "Till Difference CASH ", that is, with a trailing space. If you remove trailing spaces, your code will not work ... unless you also remove it from your test in the code.

    But as was mentioned earlier this coding causes it to run slow so is there another way to do it.
    You have been given at least three options ... and I have summarised and tested them. At least two of them use a single delete. I have taken the suggestions from Richard Buttrey and hrlngrv and refined and tested them. They both use only a single delete, although Richard's AutoFilter suggestion will require a Helper column as described by hrlngrv.

    The second example workbook has ALL the code variations. Try them and see which works best for you.

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Rows.Delete not deleting first row in range

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  17. #17
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Rows.Delete not deleting first row in range

    This should delete all columns E:K and shiftup when...
    So not delete all rows...
    But as was mentioned earlier this coding causes it to run slow so is there another way to do it.
    Making use of sample file supplied in post 14 and suggested helper column with formula
    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. [SOLVED] Delete blank rows, without deleting the rows
    By Median in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-29-2018, 07:39 PM
  2. Replies: 10
    Last Post: 06-07-2018, 04:16 PM
  3. Rows delete formula as per range of cell values with counter to delete # rows
    By qureshi2016 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-03-2017, 09:35 AM
  4. Deleting rows in table give me run time error delete method of range class failed
    By jhuvba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2015, 04:37 PM
  5. Changing code from deleting rows to cut/paste rows into another sheet and delete blank row
    By kmarshall6576 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2013, 01:54 AM
  6. Replies: 4
    Last Post: 06-11-2010, 03:29 PM
  7. How to make my macro delete rows? It currently hides the rows instead of deleting.
    By Majkataxmk in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-20-2010, 01:14 PM

Tags for this Thread

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