+ Reply to Thread
Results 1 to 6 of 6

While loop macro taking much longer than expected

  1. #1
    Registered User
    Join Date
    05-25-2021
    Location
    London, England
    MS-Off Ver
    Microsoft 365 Apps for Enterprise
    Posts
    2

    While loop macro taking much longer than expected

    Hi I created a macro to delete rows in an excel sheet that I did not want. I'll have a worksheet with 30k+ rows in it but want to reduce it to only rows that have a specific value in column L. The macro is just a simple while loop but the runtime will be 40 mins or more and I'm not sure why. Does anyone know why this could take so long? Here is the VBA code:

    Sub Delete_Ok_Rows()


    Dim Counter As Long
    Dim NumRows As Long
    Dim RowCount As Integer

    'Delete Points Classified as "OK"

    NumRows = Cells(1, 17).Value
    Counter = NumRows + 3


    While Counter >= 5

    '

    If Cells(Counter, 11).Value = "OK" Then

    Rows(Counter).EntireRow.Delete

    End If

    Counter = Counter - 1

    Wend


    'Delete Points Outside the Envelope

    NumRows = Cells(1, 17).Value
    Counter = NumRows + 3


    While Counter >= 5

    '

    If Cells(Counter, 14).Value = "Yes" Then

    Rows(Counter).EntireRow.Delete

    End If

    Counter = Counter - 1

    Wend


    End Sub

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: While loop macro taking much longer than expected

    Hi there,

    Without access to even a portion of your worksheet it's very difficult to see what's causing the delay in what appears to be simple looping code, but see if the following suggestion improves things at all:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    05-25-2021
    Location
    London, England
    MS-Off Ver
    Microsoft 365 Apps for Enterprise
    Posts
    2

    Re: While loop macro taking much longer than expected

    Sorry I forgot to attach it. Here it is
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: While loop macro taking much longer than expected


    Hi,

    your VBA procedure is just the slowest way possible …

    The kid easy & fast way is to use a helper column via a formula to mark the rows to keep or to delete via FALSE / TRUE or 0 / 1 whatever …
    Then sort the range according to the helper column in order all the rows to delete are at the end of the range,
    so easy & fast to clear the block at once rather than row-by-row !
    Processing like this manually no needs more than a minute, so imagine how fast it can be just reproducing it under VBA …

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: While loop macro taking much longer than expected

    Try this
    Please Login or Register  to view this content.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  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: While loop macro taking much longer than expected

    This question really should be a FAQ. I see it time and time again and 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.
    Then use syntax like

    Please Login or Register  to view this content.
    then paste to your destination cell.

    This uses just one Copy / Paste 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.

+ 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. Loop for Simple Macro Not Working as Expected
    By Yerda579 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-14-2019, 10:59 AM
  2. [SOLVED] For Each loop not working as expected
    By UAL225 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-17-2018, 03:48 PM
  3. Replies: 4
    Last Post: 09-08-2016, 11:13 AM
  4. [SOLVED] Code no longer works as expected
    By vbapadawan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-22-2015, 01:21 AM
  5. Loop taking much longer - looking for alternate options
    By b2b2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2012, 12:29 PM
  6. Can no longer get LOOKUP to work as expected! (2007)
    By JayUSA in forum Excel General
    Replies: 6
    Last Post: 06-17-2009, 02:25 PM
  7. [SOLVED] Macro taking longer every run
    By Ron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2006, 08:35 AM

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