Consulting

Results 1 to 10 of 10

Thread: Oddly slow running code, under very specific circumstances

  1. #1
    VBAX Newbie
    Joined
    May 2023
    Posts
    4
    Location

    Oddly slow running code, under very specific circumstances

    Hello all,

    I am working on a project generate worksheets for our lab. The code runs great, it's lightning fast, assuming you run it right after opening the workbook. For some reason if you open the work book, add or change a value for the list input and then run the VBA it takes upwards of 2 mins to run and the CPU usage spikes to around 30% according to the task manager, memory usage seems un-affected. If you make the changes, save it, re-open it and then run it. 6 seconds tops. I have tracked the issue down to the copy worksheet line using msgboxs. For some reason

    Sheets(strTemplate).Copy After:=Sheets(strLocation)
    takes forever to under these circumstances, and just adding a blank worksheet has the same issues. I have tired removing conditional formatting, suspending auto-calculations, screen updating, ensuring all named groups exist, clearing the clip board, both system and excel, and anything I can think of/google to get it to run faster. Which it does, but this issue still remains.

    I am hoping someone has some insight into how to fix this. I am really hoping that its something ridiculously easy that I am missing.

    Also I apologize for not posting any of the code but the workbook contains sensitive information that is referenced a lot. It's running on Excel 2016 64 bit, in case you need to know.

    Thanks

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    If you have any event handlers make sure you have

    Application.EnableEvents = False

    and

    Application.EnableEvents = True
    when something might trigger them

    Otherwise, you'll probably have to fake the data and post the workbook for people to look at
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    Location
    Another aspect worth considering is whether the macro has inadvertently created any excessive blank space on the 'strTemplate' sheet, particularly when it approaches a point where it takes a long time. Specifically, does the vertical scroll bar scroll far beyond the actual data on the sheet that is being relocated?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    VBAX Newbie
    Joined
    May 2023
    Posts
    4
    Location
    Thanks for the suggestion, but I am already tried that. You are correct in that I may have to post the code, which would be interesting as it's kind of a lot.

    Quote Originally Posted by Paul_Hossler View Post
    If you have any event handlers make sure you have

    Application.EnableEvents = False

    and

    Application.EnableEvents = True
    when something might trigger them

    Otherwise, you'll probably have to fake the data and post the workbook for people to look at

  5. #5
    VBAX Newbie
    Joined
    May 2023
    Posts
    4
    Location
    A good suggestion that I hadn't thought of. Unfortunately, no, the copied tab is just A1 to N21, there are a bunch of cell formulas, but automatic updates are turned off, and some conditional formatting, but removing it didn't help. The fact that the same issue happens when you insert a completely blank sheet with .add as well as with .copy seems to point at a general excel issue. That, and the fact it runs fine if you change nothing after opening it. I may, as dumb as this sounds, have to make it save, close and relaunch itself and have it auto run the code as an opening condition. It's the stupidest work around and I really don't want to do it but that may be the way to go.

    Quote Originally Posted by georgiboy View Post
    Another aspect worth considering is whether the macro has inadvertently created any excessive blank space on the 'strTemplate' sheet, particularly when it approaches a point where it takes a long time. Specifically, does the vertical scroll bar scroll far beyond the actual data on the sheet that is being relocated?

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. The WB might be currupted. Create a blank workbook and copy / paste the data and macros etc.

    2. Try cleaning the macros

    http://www.appspro.com/Utilities/CodeCleaner.htm
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Newbie
    Joined
    May 2023
    Posts
    4
    Location
    Thanks for the tip, about cleaning the macros, unfortunately its 64 bit excel so Code Cleaner is out.

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You can do it manually by exporting, removing and re-importing all the modules. Not sure it will help based on your description of the problem, but it never hurts to do on a project that has built-up over time.
    Be as you wish to seem

  9. #9
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Hi Chris83. Maybe just for funzies, trial the following code to use a collection to transfer the sheet. HTH. Dave
    Dim ShtCollect As Collection
    Set ShtCollect = New Collection
    ShtCollect.Add ThisWorkbook.Sheets(strTemplate)
    ShtCollect(1).Copy After:=ThisWorkbook.Sheets(strLocation)

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Guess you'll have to post the workbook so we can take a look
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •