+ Reply to Thread
Results 1 to 15 of 15

More efficient and faster way to delete Rows?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    More efficient and faster way to delete Rows?

    Hi All,
    I have this script that deletes the rows of records if Column A does not have any value. (Not checked).
    There are columns A - H. Column A is used for me to indicate if I want to keep the row by inputting any value.

    While it works, the issue is that it takes a long time to process and 20000 records can easily take more than 10 mins.
    Is there more efficient way that anyone knows of?

    Sub Delete()
    Dim MyRange As Range
    Dim X As Long
    Dim myLastRow As Long
    
    Application.ScreenUpdating = False
    Set MyRange = Intersect(ActiveSheet.UsedRange, Range("A:A"))
    myLastRow = MyRange.Row + MyRange.Rows.Count - 1
    For X = myLastRow To 7 Step -1
    If Cells(X, 1).Value = False Or Cells(X, 1).Value = "" Then
    Cells(X, 1).EntireRow.Delete
    End If
    Next X
    
    Application.ScreenUpdating = True
    
    End Sub
    Regards,
    Nironto

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: More efficient and faster way to delete Rows?

    Try defining "myLastRow " like so
        Dim  myLastRow As Long
        myLastRow = Range("A" & Rows.Count).End(xlUp).Row
    Then this should be redundant
    Set MyRange = Intersect(ActiveSheet.UsedRange, Range("A:A"))
    Last edited by Marcol; 06-06-2013 at 09:40 PM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: More efficient and faster way to delete Rows?

    Perhaps AutoFilter.

    e.g
    Sub test()
        With Range("a1").CurrentRegion
            .AutoFilter 1, ""
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
    End Sub
    Last edited by jindon; 06-06-2013 at 10:00 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: More efficient and faster way to delete Rows?

    Quote Originally Posted by nironto View Post
    Hi All,
    I have this script that deletes the rows of records if Column A does not have any value. (Not checked).
    There are columns A - H. Column A is used for me to indicate if I want to keep the row by inputting any value.

    While it works, the issue is that it takes a long time to process and 20000 records can easily take more than 10 mins.
    Is there more efficient way that anyone knows of?
    ...
    ...
    Regards,
    Nironto
    try this
    Sub delete_unwanted_rows()
    Dim lr&, lc&, q&
    lr = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    lc = Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column + 1
    
    With Cells(1).Resize(lr)
        .Offset(, lc - 1) = Evaluate("if(" & .Address & "="""","""",1)")
        .Resize(, lc).Sort Cells(lc), Header:=xlNo
        q = Application.Count(.Offset(, lc - 1))
        If q < lr Then Range(Cells(q + 1, 1), Cells(lr, lc - 1)).ClearContents
        .Offset(, lc - 1).ClearContents
    End With
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    Re: More efficient and faster way to delete Rows?

    Thanks Marcol,
    But if i change according to your method, it won't delete the rows.
    Basically it has to know the lastrow of records and delete only those rows that are not marked as "keep".
    I had attached the excel. In excel there are only 10000+ rows, but you can drag down to 50k plus rows to understand the pain my users undergo while waiting for it to end.

    Regards,
    Nironto
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    Re: More efficient and faster way to delete Rows?

    Hi Kalak,
    Thanks! I like your script. It is super efficient!
    But question is it will delete also the header. (The Row A1.) How do I only delete from row 2 onwards?

    Regards.
    Nironto

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: More efficient and faster way to delete Rows?

    Sub test()
        With Range("a2").CurrentRegion
            .AutoFilter 1, ""
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
    End Sub

  8. #8
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    Re: More efficient and faster way to delete Rows?

    Thanks Jindon! Tested out your script. It is simple and works beautifully!

    Regards,
    Nironto

  9. #9
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: More efficient and faster way to delete Rows?

    change is very minor (in red below). you didn't say if you had a header so I had to guess. but guessed wrong
    Sub delete_unwanted_rows()
    Dim lr&, lc&, q&
    lr = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    lc = Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column + 1
    
    With Cells(1).Resize(lr)
        .Offset(, lc - 1) = Evaluate("if(" & .Address & "="""","""",1)")
        .Resize(, lc).Sort Cells(lc), Header:=xlYes
        q = Application.Count(.Offset(, lc - 1))
        If q < lr Then Range(Cells(q + 1, 1), Cells(lr, lc - 1)).ClearContents
        .Offset(, lc - 1).ClearContents
    End With
    
    End Sub

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: More efficient and faster way to delete Rows?

    You are welcome.

  11. #11
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    Re: More efficient and faster way to delete Rows?

    This is a great site and learning experience because I get to learn so many other ways of doing the same thing.

    Hi Kalak,
    I found a issue with your script. If i select row 5-10, then is no issue.
    But if in reality, the users don't select in running sequence. If i select 5-10, then 20-25. ( Skipping some records in between)
    Then the wrong records gets deleted.

    Regards,
    Nironto

  12. #12
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: More efficient and faster way to delete Rows?

    Quote Originally Posted by nironto View Post
    This is a great site and learning experience because I get to learn so many other ways of doing the same thing.

    Hi Kalak,
    I found a issue with your script. If i select row 5-10, then is no issue.
    But if in reality, the users don't select in running sequence. If i select 5-10, then 20-25. ( Skipping some records in between)
    Then the wrong records gets deleted.

    Regards,
    Nironto
    Nironto,

    I'm afraid that I don't follow you.

    If by "select" you mean the Range(A5:A10) AND the Range(A20:25) are both empty, then that code will delete only the rows containing those empty cells and no other rows. Were you asking for something else?
    It will actually delete all rows with randomly scattered blank cells in Col A, contiguous or not, up to a million or more rows, quickly and correctly.

    If you mean something else by "select" then ...??? You didn't seem to say anything about "select" in your opening post.

    You marked the thread as solved so you're happy with it's outcome. So that's a good outcome for all involved.

  13. #13
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    Re: More efficient and faster way to delete Rows?

    Hi Kalak,
    The issue is solved but I am using your method in another spreadsheet but the lastrow of records always gets deleted.(since there is also one method provided by Jindon, thus the issue is marked as solved).

    But your method is interesting and I want to learn more from you.

    Refer to attachment. e.g I selected 12 records, but i end up with 11.


    Thanks again for you help! Appreciated.

    Regards,
    Nironto
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    09-01-2008
    Location
    Australia
    Posts
    108

    Re: More efficient and faster way to delete Rows?

    Solved it! If I changed to 2. It got it all correct!

    If q < lr Then Range(Cells(q + 2, 1), Cells(lr, lc - 1)).ClearContents
    Cheers to all!

    Regards.
    Nironto

  15. #15
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: More efficient and faster way to delete Rows?

    Thus very solved indeed.

    Anyway. Good luck!

+ 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