+ Reply to Thread
Results 1 to 9 of 9

Is there a faster way than a loop to delete rows that satisfy a criteria

Hybrid View

mortphil Is there a faster way than a... 02-28-2014, 09:10 AM
JOHN H. DAVIS Re: Is there a faster way... 02-28-2014, 09:18 AM
jindon Re: Is there a faster way... 02-28-2014, 09:29 AM
mortphil Re: Is there a faster way... 02-28-2014, 09:43 AM
mortphil Re: Is there a faster way... 02-28-2014, 09:36 AM
jindon Re: Is there a faster way... 02-28-2014, 09:49 AM
mortphil Re: Is there a faster way... 02-28-2014, 09:52 AM
jindon Re: Is there a faster way... 02-28-2014, 09:59 AM
mortphil Re: Is there a faster way... 02-28-2014, 12:14 PM
  1. #1
    Forum Contributor
    Join Date
    06-24-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2007
    Posts
    188

    Is there a faster way than a loop to delete rows that satisfy a criteria

    Hi everyone,

    I am deleting every sunday from a 5- year dates list.

    My list of date is in column "A".

    Since Sunday = weeday("date") -> 7

    I do the following:

    sub delete_sunday
    
    for x = 1 to cells(rows.count,"a").end(xlup).row
    
      If weekday(cells(x,"A").value) = 7 then
        rows(x).delete xlshiftup
        x= x -1 
      end if 
    
    next x
    This works, but it takes a while, do you know another way to delete things within a range that is quicker?

    Thanks for your time,

    -Phil

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

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    You could turn your screenupdating off.

    Application.screenupdating = false

    YOUR CODE

    Application.screenupdating = true

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

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    7 for Sunday?
    1 for Sunday to me...
    Sub test()
        With Range("a1", Range("a" & Rows.Count).End(xlUp))
            .Value = Evaluate("if(weekday(" & .Address & ")=7,""""," & .Address & ")")
            .SpecialCells(4).Delete xlShiftUp
        End With
    End Sub

  4. #4
    Forum Contributor
    Join Date
    06-24-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2007
    Posts
    188

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    Jindon: your solution took 40 seconds. that is awesome.


    now, I need to understand it.

    1) I get the first part With "a range"
    2) the second line I don't entirely understand it. I have never used EVALUATE
    3) then you cleared any empty cell within the range using specialcells(4).delete

    thanks a lot for your solutions guys
    -Phil

  5. #5
    Forum Contributor
    Join Date
    06-24-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2007
    Posts
    188

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    Thanks guys.

    I put a timer on my code and here is my results:
    On a total of 1,304 dates it tooks
    1) updatescreen turn on = 01:56
    2) updatescreen turn off = 01:46

    Ps my computer is not very fast, though!

    Sorry
    Jindon: you are right sunday = 1 not 7. let me try your solution.

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

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    40 Sec?

    How many rows do you have?

    I tested with 4000 rows and took only 0.5 sec.

  7. #7
    Forum Contributor
    Join Date
    06-24-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2007
    Posts
    188

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    only a 1000 rows, let me try that again. I will first kill any other software and repopen excel.

    by the way, I am impress this evaluate method I need to get familiar with it

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

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    Try add two lines
    Sub test()
        Application.Calculation = xlCalculationManual
        With Range("a1", Range("a" & Rows.Count).End(xlUp))
            .Value = Evaluate("if(weekday(" & .Address & ")=7,""""," & .Address & ")")
            .SpecialCells(4).Delete xlShiftUp
        End With
        Application.Calculation = xlCalculationAutomatic
    End Sub

  9. #9
    Forum Contributor
    Join Date
    06-24-2013
    Location
    Berlin
    MS-Off Ver
    Excel 2007
    Posts
    188

    Re: Is there a faster way than a loop to delete rows that satisfy a criteria

    that is even faster, Thanks !!

+ 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] More efficient and faster way to delete Rows?
    By nironto in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-07-2013, 05:01 AM
  2. Delete the rows of a specific account if a certain column does not satisfy
    By exlgh91 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2013, 04:10 PM
  3. Loop through rows and delete based on criteria
    By yolandaro30 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-27-2012, 12:23 PM
  4. Loop to delete rows meeting criteria
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2009, 04:54 PM
  5. How to loop through worksheet and delete rows based on criteria
    By rgrogan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-20-2007, 05:28 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