+ Reply to Thread
Results 1 to 3 of 3

Macro with 3 very similar complex loops run continuously

  1. #1
    Registered User
    Join Date
    08-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    39

    Macro with 3 very similar complex loops run continuously

    Hello,

    When I first started using VBA, loops seemed so simple! Now I recently got help on consturcting a more complex loop and I made two minor alterations to achieve a set of data processing goals.. but the loop(s) run continuously, and this one is over my head; I would greatly appreciate your time and expertise.

    Through out the code in the attached file, I labeled the loops as "LOOP 1", "LOOP 2", "LOOP 3"

    The first loop finds a row with a "UE" doc type, then tries to find another row with a numerical amount that is the negative equivalent in column T; If the original record is $100, then it tries to find -$100. If it find one or more matches, then the macro looks through those matches to find a record that meets further criterion.

    The second and third loops (Step 14 and 15) follow this same logic.. but are slightly different..

    Here is the code of the first loop:

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    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: Macro with 3 very similar complex loops run continuously

    Wow!

    That's some macro. I don't think it's the case that your macro is running continuously if by that you think it's got stuck in an infinite loop. The macros is certainly running continuously - that's what it's supposed to do. However when you get to the loop

    For z = 2 To LR

    it's processing over 5000 rows from circa 2157 to 7419. On my reasonably fast laptop the loop is taking 3 seconds so with over 5000 iterations it's going to take over 4 hours.

    I can't help thinking that you perhaps need to rethink the code.

    For a start try

    1. Breaking it down into smallet procedures so that it's more easy to read and control.
    2. Don't Activate (or select) cells. Every time you do that there's a time overhead involved in VBA jumping back to the Excel App, and then jumping back. If you're processing large ranges - as here, read them into a VBA array(s) and process them entirely in VBA only writing the final resultant array back to Excel at the end.
    3. The fastest way I know of processing stuff like entering values to specific cells (or deleting rows) is to use Data Filtering rather than loop procedures. Whenever you think about creating a loop think first whether there is a better way. A filter is extremely efficient and when you use syntax like
    Please Login or Register  to view this content.
    you can update whole blocks of cells instantaneously. So where you are detecting whether F,V,X & L cells contain stuff, and then making column 32 = 40, a filter could do that in one hit.
    4. Try and avoid 'spaghetti' code where you jump out of loops (Goto Skip_Me). I don't think that's a particular problem here but one day it will catch you out and it tends to make code hard to read.
    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.

  3. #3
    Registered User
    Join Date
    08-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Macro with 3 very similar complex loops run continuously

    Mr. Buttrey,

    Thank you very much for your expertise. I have moved on to get help implementing these recommendations.

    Adam

+ 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] Macro with 3 very similar complex loops run continuously
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-21-2014, 09:25 AM
  2. Replies: 17
    Last Post: 08-18-2013, 03:29 AM
  3. Macro running continuously
    By billpurdom in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2013, 12:29 PM
  4. Continuously running a recorded macro-Please Help
    By Nik.Kaps in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-10-2012, 07:27 AM
  5. Replies: 4
    Last Post: 02-03-2010, 07:15 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