+ Reply to Thread
Results 1 to 11 of 11

Improving Macro's efficiency

  1. #1
    Registered User
    Join Date
    01-17-2023
    Location
    California
    MS-Off Ver
    2019, 365
    Posts
    7

    Improving Macro's efficiency

    I have been using a very lengthy macro weekly for the past 3-years. The macro takes about 7 1/2 hours of computer time to run. I am not having success in combining steps within the macro to reduce the time it takes to run this macro. Below is a representative sample of the inefficient and working script in standard font. This is followed by how I thought steps could be combined in bold font. Unfortunately I cannot get the combined step macro to work. Any input on what I am doing wrong would be greatly appreciated. This could be a specific example or generally what I'm doing wrong, or what reference would be the most useful.

    Please Login or Register  to view this content.
    Thanks in advance.
    Last edited by 6StringJazzer; 12-27-2023 at 09:41 PM. Reason: please use code tags

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Improving Macro's efficiency

    Your original code looks like it came from the macro generator, which generates crude but effective code.
    I cannot get the combined step macro to work
    You would have to be specific about what you want to happen, and what happens instead. Your original code is difficult to reverse engineer, and if your new code doesn't do what you want, it's not helpful.

    Also, although the original code is clunky, it does not look like it should take hours to run. How long is the complete code? Can you provide an Excel file so we can run the code, or at least see all of the code? I suspect you have a problem in another part of your code that is not included here.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-17-2023
    Location
    California
    MS-Off Ver
    2019, 365
    Posts
    7

    Re: Improving Macro's efficiency

    Yes the original code did come from Excel 2016 macro recorder and then cleaned up some. An interesting twist is despite being written by Excel 2016 I use Excel 365 to run the code because the macro has never worked on Excel 2016 but has work for 3 years using Excel 365. I would think it would work on the version that it was written on before it would work on a different version.

    The reason the code takes so long is it is looped with different variables. First it is looped for 39 different geographic areas, then that is looped for 14 weeks and then it is looped for offers versus closings. So the basic script is looped just shy of 1,100 times. Saving 1 second ends up saving nearly 20 minutes off the 7 1/2 current runtime.

    I have a sheet "Data" that has about 25,000 records each in row and 15 data point each in a column A-O. I filter this data by closing date, copy and paste into a sheet "Working Data." "Working Data" now contains 1 of 14 weeks worth of data. I filter "Working Data" and select and copy the 1st of 39 geographic areas and copy and paste this into "Calculations" starting in cell A10. I then copy the first 9 rows from "Formulas" and paste starting in cell A1 of "Calculations." I then copy from "Calculations" cells F3 through BI and paste the values into 1 of 39 geographic area sheets starting in column F and the row assigned for this current week is 572. This then loops back and I sort for the 2nd of 39 geographic areas. Then I loop for the remaining 13 weeks. Then I loop back to "Data" and repeat the entire process filtering by offer date.

    I am show the steps that I thought could be combined from multiple simple singles line to a more complex single line. Basically I thought I should be able to specify the sheet, the location, and the desired operation all in one line separated by periods.

    In addition to improving the efficiency of the macro code, guidance on what would reduce the time of the current clunky macro. Faster CPU, more RAM. I currently use Intel i7-8700 CPU3.20 GHz, 16 GB RAM, Windows 10 Home, 64 bit. The data file is about 65 MB in xlsb and the macro file is 33 MB in xls.

    Here is the working code to set up and perform desired operations through the 1st loop of roughly 1,100 loops.

    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 12-28-2023 at 10:07 AM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Improving Macro's efficiency

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however we recommend that you include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Guideline #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here.

    I added them but please do this in the future.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Improving Macro's efficiency

    But you're not showing us the loops. Please show all the code.

  6. #6
    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,464

    Re: Improving Macro's efficiency

    I would start with this:

    Please Login or Register  to view this content.
    Avoid using .Select ... Selection.

    This:
    Please Login or Register  to view this content.
    Could be this:
    Please Login or Register  to view this content.
    As has been said, it's difficult to know how best to improve the code without knowing the expected outcome.

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    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


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

    Re: Improving Macro's efficiency

    Is this resolved now? Or do you need more help?


    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.

  8. #8
    Registered User
    Join Date
    01-17-2023
    Location
    California
    MS-Off Ver
    2019, 365
    Posts
    7

    Re: Improving Macro's efficiency

    This information was fantastic. I am estimating that I reduced macro run time from 7 1/2 hours to just 1 1/2 hours.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Saved about 1 hour of run time and was super easy to implement. There was an additional benefit in that becauseI turned ScreenUpdating for the entire time, I moved other database initialization
    steps to an startup section. This allowed me to initial the database once instead of for each of 28 weekly runs. This improved programming saved more time.

    Please Login or Register  to view this content.
    Also saved significant amount of time. This was more challenging to implement in that I initially turned in on for just the one step where the calculations are actually done. Part that did not work initially. I got it working by also having this feature in Automatic mode when a moved the calculated values to the output spreadsheet. Likely because I am pasting both values and format.

    I implemented the With Sheets suggestion. Not sure why it worked for some command and not others.

    Despite the HUGE improvement in reducing the time to run the code, I would still like to know if there are any time saving technique for these particular commands.

    Please Login or Register  to view this content.
    I don't understand why

    Please Login or Register  to view this content.
    did not seem to work. If it is pilot error and this should work that would be good to know. I use this almost 3,300 times in the 90 minute run time, so any time savings would be significant.

    Seems like there should also be a better way to reset consecutive data filters. I am currently using

    Please Login or Register  to view this content.
    Thanks for all the input. Very pleased with the improvement just trying to ensure there is no more improvements to make.

    There was also some interest in the complete code being used. I would be happy to share if would help someone. This would be very very long and because it is repetitive, I will start by sharing:

    1) The part that get repeated 39 for each of the 39 different areas.

    2) The initialization of the database that is now only used once at the start of each of 28 different weeks.

    3) The master macro that calls each of 28 different macros. I also reduced speed by commenting out some of the older (lower numbered) weeks because the number of updates is reduced as the data ages.

    If I can help provide more information, just let me know. The balance of this post is the code mentioned above.

    1)
    Please Login or Register  to view this content.
    2)
    Please Login or Register  to view this content.
    3)
    Please Login or Register  to view this content.

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

    Re: Improving Macro's efficiency

    See this for a start: https://techcommunity.microsoft.com/...nce/m-p/173687

    And Google: for variations on a theme.

  10. #10
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Improving Macro's efficiency

    Seems like there should also be a better way to reset consecutive data filters.
    Please Login or Register  to view this content.


    Sheets("Calculations").Select
    Range("A1").Select
    ActiveSheet.Paste

    Either:
    Please Login or Register  to view this content.
    Or:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-26-2023
    Location
    Oklahoma
    MS-Off Ver
    365
    Posts
    57

    Re: Improving Macro's efficiency

    I haven't quite grasped everything that your program is trying to do; nonetheless, I can offer this advice which I guarantee will speed your operations tremendously. Instead of performing numerous operations via worksheet manipulations, read the entire data into arrays or into collections. Perform all the calculations and manipulations, then write the results back to the appropriate worksheets. Even better would be to create class modules so you can more intuitively manipulate the data.

+ 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] Improving Macro Efficiency - taking hours to run
    By greenstar in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-24-2021, 12:44 PM
  2. Request In Improving Macro
    By crifat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-03-2019, 06:59 PM
  3. Improving this macro?
    By terrypin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-23-2018, 08:28 AM
  4. Improving formula/workbook efficiency
    By aquixano in forum Excel General
    Replies: 0
    Last Post: 06-24-2016, 05:53 PM
  5. Need help improving Job efficiency
    By popovgor in forum Excel General
    Replies: 5
    Last Post: 03-07-2014, 01:11 PM
  6. Replies: 2
    Last Post: 01-26-2013, 04:26 AM
  7. improving speed and efficiency
    By wishmaker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2010, 11:08 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