+ Reply to Thread
Results 1 to 9 of 9

Loops are too Slow.

  1. #1
    Registered User
    Join Date
    05-06-2012
    Location
    Raleigh, North Carolina
    MS-Off Ver
    Excel 2010
    Posts
    5

    Loops are too Slow.

    Hello everyone!
    I am trying to evaluate a set of records to determine if they should be posted to a display. Then evaluate a second set of records to either, post next to the prior month with the same item (project), fill in a blank space for that area (department), or add a new line to that area (department).

    I can do the basics and have it looping through and it works! The problem is that it takes about 38-40 to run everything and it seems like ~30 seconds to run these loops (for an example of 47 months, 36 records to be read, and a starting display table of about 25 rows and after the macro with about 33 rows). I am posting the basic idea immediately below.

    Read from table:
    1.) Read each record.
    2.) If that record has a 'True' indicating it should be posted for that month (from a formula reading a date that changes for each month) we enter another loop for placing it.
    3.) We read each record on the table for placement. We check to see if the department matches. When the department matches, we check to see if the same project was listed in the prior month. If it was, we enter the information next to the same project.
    4.) If the project does not match the prior month, then we loop through the display table again. When we find the same department, we find the next empty cell to enter the project.
    5.) If there are not enough empty cells for that department, then we loop through the display table again. We find the last row in the display table for the same department. Then we add a row and enter the project.
    6.) There is an additional loop to represent months that carry forward into future years.

    Then we go back and read the next record to be evaluated. The current year (8 months) seems to take all the time and that is where most of the records will be displayed. It seems to pass through the loops very fast for the future years with very little being posted (hence not looping on the display).

    Is there ANY way to complete the same actions and greatly reduce the time to post each project in the correct spot on the display? I have attached a copy of the loop hoping that would be enough information to evaluate. The I have about 9 subroutines being called but everything else is at most a second (maybe two). I am hoping to be able to run the macro when the workbook opens.

    Thank you!
    Attached Files Attached Files
    Last edited by AltNrg4U; 05-06-2012 at 02:36 PM. Reason: Add additional info.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Loops are too Slow.

    Not sure what you expect anyone to do with a word document containing your VBA code ... which is hard to read, and nothing to test it with.

    You may generate more interest if you post a sample workbook with some typical data and the full code in context.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-14-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Loops are too Slow.

    Hiya

    I don't know how much help I can be as I can't actually open the file for your code, but from experience I found that sometimes use the macro to take a copy of your list and delete those cases you don't need so that you are looping through a much shorter overall list?

    Hope this is of some help to you

  4. #4
    Registered User
    Join Date
    05-06-2012
    Location
    Raleigh, North Carolina
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Loops are too Slow.

    TMShucks,
    Thank you so much. Your advice makes a lot of sense and I thank you. I am unsure how to proceed due to a confidentiality agreement. This is my first time asking for help so I was unfamiliar with the protocol and you helped.

    Having documented this, I have formulated a different approach that I could not see when I started. I think I can create an additional formula column that lists the number of months in the range, so that (if it works correctly), I don't loop for each month, but only once (on the monthly loop). Thus dropping the overall loops to 1 for reading and 3 for displaying. I will update my results.

  5. #5
    Registered User
    Join Date
    05-06-2012
    Location
    Raleigh, North Carolina
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Loops are too Slow.

    LouiseH24,
    Thank you for your assistance. I have posted an update to TMShucks which is relevant. I had deleted everything except what was going to be displayed, it was only determining which months they would display. Having written this out and thinking further, I believe that I have a possible solution. I will update the results.

    I appreciate your feedback.

  6. #6
    Registered User
    Join Date
    05-06-2012
    Location
    Raleigh, North Carolina
    MS-Off Ver
    Excel 2010
    Posts
    5

    Talking Re: Loops are too Slow.

    I am still reviewing the whole project for improving the speed, but I did help a lot. I improved it from ~38 seconds to ~12 seconds (but increased additional code to get it back up to 20 seconds).

    I was looping through 38 records (Projects) and comparing and placing it on a display that went out to 2016 in my test. The display started with 25 records and ended around 33 records.

    Old Method
    1.) I would loop, for each Project, through every Project listed on the Display each month to determine if it matched the prior month Project. If it did, I updated the Display for that month and put a flag on that Project for that month. (The flag would prevent that Project from being examined in following loops for that month.)

    2.) If there was not a match, I would loop, for each Project that was not flagged, through every Project listed on the Display each month to determine if the Project's Department had an empty cell for that month. If there was a match, I placed it in the first empty cell for that Department and put a flag on that Project for that month.

    3.) If the Project's Department did not have any empty cells for that month, I would loop, for each Project that was not flagged, through every Project listed on the Display each month to find the last record in the Display for that Department. I would then insert a record into that Department and copy the Project into the empty cell that was just created for that month. I would then put a flag on that Project for that month.

    4.) If the Project's Department did not exist in the current Display, I would create a record at the bottom of the Display to create a new Department. I would then insert a record into that Department and copy the Project into the empty cell that was just created for that month.

    5.) The flags would be removed and we would start all over for the next month.

    New Method
    ***) I completed more calculations in hidden columns to determine 1) how many months, from the current month, until the Project Started, and 2) how many months would the Project continue from that start month.

    1.) I then followed all four steps above. I changed the fact that I was updating a flag and I only completed each set of loops once instead of for every month.

    This saved me about 20 seconds for a relatively small number of records being examined.

    I am not sure if this is beneficial to anyone else, but since I asked the question, the least I could do was post my solution.

    Thanks for the assistance.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Loops are too Slow.

    Whilst it may not be possible or advisable to upload the actual spreadsheet, if you can anonymise the data but retain the structure, volume of data and code, and upload a sample workbook, we may be able to improve on the performance.

    Failing that, it may be possible to offer advice on optimising the code if you just upload that (bearing in mind you must use code tags).

    If that's not possible, thanks for sharing your solution/progress and good luck with the project.

    Regards, TMS

  8. #8
    Registered User
    Join Date
    05-06-2012
    Location
    Raleigh, North Carolina
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Loops are too Slow.

    TMShucks,
    Thank you for the kind offer. I will take you up on it, but right now my daughter is graduating and family is coming in from all over. I will provide a cleansed copy on Sunday or Monday. Enjoy your weekend.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Loops are too Slow.

    Good to find someone whose life and priorities don't revolve around Excel ... although I don't know how that can be

    Enjoy your time with the family and I hope the ceremony is memorable for your daughter. I wish her well in her future.

    You have a good weekend too.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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