+ Reply to Thread
Results 1 to 25 of 25

Macro Speed Varies

  1. #1
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Macro Speed Varies

    Greetings,

    I have a macro that I run in excel 2007 and for some reason it will vary in speed. I have a large excel file with over 30,000 rows that need to be updated by this marco. Yesterday when running it, it was completing about 100 lines per minute. Now it is completing only about 10 lines per minute. Yup, it's running 10x slower than it did yesterday. I tried saving the file and closing out excel, then reopening. That didn't work. Then, I tried shutting the computer down and opening up the file. Still didn't work. So my questions are:

    1. What controls this seemingly random change in speed?

    2. How can I get my macro to run FAST every single time?

    As you can imagine, updating 30,000 lines takes a long time with this complex macro, but it is better to run 100 a minute than 10 a minute.

    Any ideas what I can do to remedy this issue?

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

    Re: Macro Speed Varies

    1. What controls this seemingly random change in speed?
    Who knows? It doesn't sound as though it is random. If it was a 100 lines yesterday, and 10 lines now, but it won't go back to 100 ... probably not random, probably broke. Guessing you have done something inadvertently. Have you added any array formulae? Or SUMPRODUCT? Do they have full column references? Have you copied a formula (or two, or three) down to the bottom of the column(s)? Have you introduced any Conditional Formatting? Does any Conditional Formatting apply to solid blocks of cells? Or is it split into many areas? And possibly duplicated?

    2. How can I get my macro to run FAST every single time?
    Do you switch calculation to manual before and automatic after? Do you switch off screen updating before and on again after? Do you have any event handlers, particularly Change and Selection_Change? Do you switch off event handling before and on again after? Do you select sheets, columns, rows or cells, in order to work with them? That is do you have structures like:

    Please Login or Register  to view this content.
    If you do, they can be combined:

    Please Login or Register  to view this content.

    Other than that, it's difficult to guess at how to improve it and, ideally, see it working in a sample workbook.

    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
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Macro Speed Varies

    100 lines per minute is still incredibly slow. Chances are that if you post a workbook with the code and some data (use mockup data if you have some sensitive data) it can be made to run much faster.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro Speed Varies

    To add to TMS's excellent comments;
    Without knowing specifics of what your macro is doing, even 100 lines\minute is considered very slow. There are likely ways to speed it up (TMS's suggestions not withstanding). If you want to post your code and explain what it does, I'm sure you'll get additional recommendations to improve performance.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    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,463

    Re: Macro Speed Varies

    @AlphaFrog: thank you, I do my best ... but, as you say, without seeing something to assess, it's difficult to be specific. And there may be other issues when it's staring you in the face. Equally, often it's possible to read all the data into an array and process it all in memory. Some coders are more adept at that approach than others (like me) who tend to stick with processing on the worksheet but try to minimise the slow elements.

    Regards, TMS

  6. #6
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    Thank you all for the suggestions. Attached is my code. I did not write it, and I am a total novice. It calculates football statistics. Please find my attached code and let me know if someone can help me fix it.

    Thanks!

    Dan
    Attached Files Attached Files

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

    Re: Macro Speed Varies

    There's a lot of code to assess but, as suggested, there's quite a bit of this:

    Please Login or Register  to view this content.
    which you could reduce to this:

    Please Login or Register  to view this content.
    or even:

    Please Login or Register  to view this content.

    So, a) you don't select the cell in order to manipulate it and b) you don't use an intermediate variable to store the formula.

    You also have commented out the ScreenUpdating = False and ScreenUpdating = True which won't help. It shouldn't cause a calculation problem as it's just preventing the screen being redrawn, not stopping cell values being updated.

    I can't see that you switch calculation to manual so that's likely to slow it down.

    But, in my opinion, the chances are that the biggest problem is the Worksheet_Change event handler. You are putting a formula in a cell in the event handler, hence each change (to the cell's formula) will fire the Change event again ... potentially causing a loop.

    We can't assess any issues with the actual workbook/worksheet(s) because we don't have access to it.

    Regards, TMS

  8. #8
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    So what do you recommend I do?

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

    Re: Macro Speed Varies

    Not sure how to answer that, at least any differently than the explanation of what (I think) is wrong.

    In the Change event handler, you need to set Application.EnableEvents = False before making a change within the event handler. And then Application.EnableEvents = True after making the change.

    Similarly, Application.ScreenUpdating = False at the start of your code and Application.ScreenUpdating = True at the end.

    And you can set Application.Calculation = xlCalculationManual at the start of your code and Application.Calculation =xlCalculationAutomatic at the end. Given the apparent complexity of your calculations, it might be worth being a little circumspect about this. Switch it to manual before specific sections of code and to automatic after to ensure that calculations are done appropriately.

    And I've already described how not to select cells.

    Regards, TMS

  10. #10
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Macro Speed Varies

    Hi dcebb2001,

    Before making any changes, I would suggest the following as a baseline test.
    a. Reboot the computer
    b. Open the Excel file and run the Macro as soon as the squirrels inside Windows stop running at startup.
    c. Run the macro several times, and identify when performance degrades.

    I have run into similar problems that were solved after the computer took a short nap.

    Lewis

  11. #11
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    Thanks.

    @ LJ - I did restart it earlier and it didn't help. Still slow.

    @TMS - Based on what you have seen in my code, what would you change? I don't want the darn thing to break and I am very uncertain as to what to do. I have never coded in VB before, so giving me a procedure would help. In post #7, you suggest that I change those lines to the 2nd or last option you present. Is one better than the other? If you were asked to optimize this, what would you do, in order, so I can make the changes to a sample file and see if they work. I am a novice, so listing line numbers would help.

    Thanks!

  12. #12
    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,463

    Re: Macro Speed Varies

    @Lewis: whilst I totally agree with your suggestion, there are issues with the code that I think would make it relatively slow at the best of times. What I can't guess at is what has changed to make the code an order of magnitude slower. A reboot might just address that

    Regards, TMS

  13. #13
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Macro Speed Varies

    A few basic ideas.

    A basic divide and conquer tactic with code is to put something like this in several places:
    Debug.print now & "some description"
    Vary "some description" for different instances. Maybe format that Now as well.

    Then you may be able to finger which line of code is slow.

    This not necessarily going to even help, but it might focus your efforts to an identifiable culprit.

    Also consider looking at TASKMAN or some better CPU usage tool to see if you're getting cycles.

    And what LJMetzger said.

  14. #14
    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,463

    Re: Macro Speed Varies

    Please post a sample workbook with a representative example of the data. Only a few 100 or even a few dozen rows of non-sensitive is necessary. The intention is not to test the speed, it's to make sure the code compiles.

  15. #15
    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,463

    Re: Macro Speed Varies

    Oh, and the code all needs to be where it should be

  16. #16
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    Tried to upload it.

    Got this message:

    The following errors occurred:

    All Data Sample.xlsm: Your file of 11.49 MB bytes exceeds the forum's limit of 1,000.0 KB for this filetype.


    Seems like it is too big to attach.

  17. #17
    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,463

    Re: Macro Speed Varies

    Try zipping it up. Failing that, try DropBox or similar.

    The size of the file is indicative of the problem. It will be interesting to see. Is this the actual file? No sensitive data?

  18. #18
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    I can't attach a zip. Says that the file type can't be uploaded. It was a .rar.

    The following errors occurred:

    All Data Sample.rar: Invalid File


    This is the one of the sheets - the only sheet that uses anything in the macro. All the sensative data has been removed. Only columns with anything regarding the macro are not cleared out. How else can I make it available for download?

  19. #19
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies


  20. #20
    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,463

    Re: Macro Speed Varies

    I've made changes to the code but, unfortunately, I just can't run the code on my machine. It may be running correctly but I'm not prepared for my laptop to be locked up for an unknown amount of time.

    I've made the amendments I suggested.

    My new concern is that the code uses SendKeys. Seriously not a fan and I have no idea whether or not it is necessary. If my understanding is correct, it is entering Edit mode and the pressing Enter

    Please Login or Register  to view this content.

    Why would you do that?


    Whatever, I suspect that I cannot provide any further help.


    Regards, TMS
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    The program definitely ran faster. However, it didn't do the calculations correctly. I don't know why. Ran fast though.

  22. #22
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    Also - can you explain the whole sendkey thing to me? I have no idea what those do. If they are bad, can I remove them?

  23. #23
    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,463

    Re: Macro Speed Varies

    I'm away for a few days so I'm not in a position to look at it in any detail. However, faster but wrong is not a good outcome.

    I'd try commenting out the Application.Calculation = xlCalculationManual at the start and see if that helps.

  24. #24
    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,463

    Re: Macro Speed Varies

    SendKeys does what it says ... sends keystrokes to the active application. It can be very unreliable with unexpected results and very risky.

    F2 causes Excel go into Edit mode and, when keyed manually, you'd see the cursor in the formula bar at the end of the text, value or formula. The tilda (~) is the equivalent of pressing Enter to re-commit a formula or re-enter data and I'd expect it to cause Excel to calculate.

    So, imagine selecting cell A2, pressing F2 to enter Edit mode, and pressing Enter. The cursor would move down to the next row/cell. Repeat 30,000 times and move across 1 column ... repeat for 200 columns.

    Not sure if it's doing it for every cell but it's not good. Could explain why things are slow. But it could be there with the intention of forcing re-calculation.

    Whilst I can suggest performance improvements, I don't have an appreciation of the logic or calculations or what issues the use of SendKeys was intended to overcome. Consequently, I don't know what would happen if you were to comment the lines out and test the code without them.

    Regards, TMS

  25. #25
    Registered User
    Join Date
    01-29-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Macro Speed Varies

    I would be more than happy to explain the logic if that helps. I just would like this thing to run faster and correctly. I know, easier said than done.

    Basically, columns DR:FH contain values that do not change. Column U determines the player's position. Column AC determines his "Target" overall rating. The goal, is to manipulate certain values in DR:FH to change the result of a formula called in column FK. Due to me wanting to keep DR:FH constant, columns FL:HB are representative of these values, but are modified by a value in column FJ.

    So, to put it simply:

    1. U determines the formula in FK that is used.
    2. U determines the value in M.
    3. The numeric result of the formula in FK needs to match that of AC.
    4. The values in FL:HB are the same values as DR:FH + the value in FJ (both positive and negative) in order to try to get FK to equal AC.
    5. Only selected cells in FL:HB are changed based on the formula of FK.
    6. FJ is changed in intergers of 1 either positively or negatively to change those selected values in FL:HB.
    7. The resulting changes in the values of FL:HB allows the value of FK to change so that FK = AB.
    8. If changing these values cannot be attained to get to the value in AB, then FI is further modified until FK equals AB.


    I know, it may not make much sense, but that's what the code does. Any help would be awesome when you or anyone else gets a chance.

+ 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] Using a Macro to format spreadsheet - warning, there's a twist number of columns varies
    By MagicMan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-30-2015, 10:52 AM
  2. Everage Speed km/time (european speed)
    By GerryZucca in forum Excel General
    Replies: 3
    Last Post: 02-23-2015, 03:02 PM
  3. [SOLVED] How to Speed up my macro
    By manofcheese in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2014, 02:17 PM
  4. [SOLVED] Are you able to help Speed Up this Macro?
    By Andrew.Trevayne in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2012, 03:31 AM
  5. [SOLVED] How To Speed up macro
    By AnkitGuru in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-02-2012, 06:36 AM
  6. macro where array varies
    By LiamF in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-27-2008, 05:39 AM
  7. Macro to find a range that varies.
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-29-2005, 07:40 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