+ Reply to Thread
Results 1 to 10 of 10

Code running really slowly

Hybrid View

The Phil Code running really slowly 04-03-2014, 01:05 PM
Leith Ross Re: Code running really slowly 04-03-2014, 01:24 PM
The Phil Re: Code running really slowly 04-03-2014, 01:35 PM
romperstomper Re: Code running really slowly 04-03-2014, 01:29 PM
The Phil Re: Code running really slowly 04-03-2014, 01:41 PM
romperstomper Re: Code running really slowly 04-03-2014, 05:53 PM
jolivanes Re: Code running really slowly 04-03-2014, 07:00 PM
The Phil Re: Code running really slowly 04-03-2014, 07:24 PM
jolivanes Re: Code running really slowly 04-03-2014, 08:42 PM
The Phil Re: Code running really slowly 04-03-2014, 07:19 PM
  1. #1
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Code running really slowly

    I'm running the following code on about 8,000 rows of data. The data has been pasted as values so there are no formulas being affected. This code deletes every row where the value in column E is zero.

    When running this, Excel just hangs for a long time, till I finally just killed the program. I copied about 10 rows to another sheet within the workbook, and only 2 of those rows should have been deleted by this macro. I ran the macro again on my smaller test size sheet and it worked, but it took 20 seconds....

    Is there any way to speed this up?

    Note: I have a new I5 processor with 8GB of ram, I don't think this is an issue with computer power.

    Sub Caseware()
    
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim i As Long
    
    For i = Cells(Rows.Count, "a").End(xlUp).Row To 2 Step -1
    If Cells(i, "e") = 0 Then Rows(i).Delete
    Next i
    
    End Sub

  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,259

    Re: Code running really slowly

    Hello The Phil,

    I have an idea that might work. Are there any formulas in column "E"?
    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
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Code running really slowly

    Quote Originally Posted by Leith Ross View Post
    Hello The Phil,

    I have an idea that might work. Are there any formulas in column "E"?
    There are no formulas on that sheet at all. Though there are lots of other sheets with lots of complex formulas, the recalculate is set to manual so that shouldn't matter.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Code running really slowly

    It really shouldn't be that slow - do you have any event code in the sheet?

    Probably quicker to only delete once:
    Sub Caseware()
    Dim rngtodelete as range
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim i As Long
    
    For i = Cells(Rows.Count, "a").End(xlUp).Row To 2 Step -1
    If Cells(i, "e") = 0 Then
    If rngtodelete is nothing then
    Set rngtodelete = cells(I, " e")
    Else 
    Set rngtodelete = union(rngtodelete, cells(I, "e"))
    End if
    Next i
    If not rngtodelete is nothing then rngtodelete.entirerow.delete
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Code running really slowly

    Quote Originally Posted by romperstomper View Post
    It really shouldn't be that slow - do you have any event code in the sheet?

    Probably quicker to only delete once:
    Sub Caseware()
    Dim rngtodelete as range
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim i As Long
    
    For i = Cells(Rows.Count, "a").End(xlUp).Row To 2 Step -1
    If Cells(i, "e") = 0 Then
    If rngtodelete is nothing then
    Set rngtodelete = cells(I, " e")
    Else 
    Set rngtodelete = union(rngtodelete, cells(I, "e"))
    End if
    Next i
    If not rngtodelete is nothing then rngtodelete.entirerow.delete
    End Sub
    Hi,

    I went to try this but got a compile error on the row "Next i". I'm not sure how to fix that.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Code running really slowly

    Sorry - missed an end if:
    Sub Caseware()
    Dim rngtodelete as range
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim i As Long
    
    For i = Cells(Rows.Count, "a").End(xlUp).Row To 2 Step -1
    If Cells(i, "e") = 0 Then
    If rngtodelete is nothing then
    Set rngtodelete = cells(I, " e")
    Else 
    Set rngtodelete = union(rngtodelete, cells(I, "e"))
    End if
    End if
    Next i
    If not rngtodelete is nothing then rngtodelete.entirerow.delete
    End Sub

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Code running really slowly

    Just thinking out loud here.
    Would this work?
    Sub Try()
        With Range("E1:E8000")    '<---- Change as required
            .Replace 0, "", xlWhole
            .SpecialCells(4).EntireRow.Delete
        End With
    End Sub

  8. #8
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Code running really slowly

    Quote Originally Posted by jolivanes View Post
    Just thinking out loud here.
    Would this work?
    Sub Try()
        With Range("E1:E8000")    '<---- Change as required
            .Replace 0, "", xlWhole
            .SpecialCells(4).EntireRow.Delete
        End With
    End Sub
    I tried this on a sample sheet with 16 rows, 4 of which should have been deleted. It worked but it took over 30 seconds to run.

    It's looks like there's something wrong with the workbook itself. If I make a new workbook and run your script, then it works pretty much instantly....

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Code running really slowly

    What kind of formulae do you have in the workbook?
    I had a similar problem years ago. Some functions recalculate whenever a change is made.
    I changed the culprits and the problem was solved.
    I personally don't know anything at all about these functions but if you post the formulae someone might be able to give you an alternative.

  10. #10
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Code running really slowly

    Quote Originally Posted by romperstomper View Post
    Sorry - missed an end if:
    I should have mentioned this, I tried adding an end if because I saw that too, but then I get the following error:

    Run-time error '13':
    Type mismatch

+ 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] Code running slowly
    By Sweepin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-30-2013, 09:15 PM
  2. [SOLVED] Code running slowly any way of speeding up processing time
    By boomboomblock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2013, 09:09 AM
  3. Code running slowly
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-01-2011, 11:08 AM
  4. Replies: 3
    Last Post: 11-24-2010, 03:43 AM
  5. Paste code running extremely slowly...
    By KR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2005, 11:05 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