+ Reply to Thread
Results 1 to 10 of 10

VBA Code is running really slowly

  1. #1
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    VBA Code is running really slowly

    Hi,

    I trying to make a macro that does some copying and pasting between worksheets, but for some reason it is running really slowly. it also seems to get slower with time, if I try to run a 2nd and 3rd time to the point that the code doesn’t completely run at all, and I have to Ctrl+Alt+Delete, just to be able to close excel.

    I think it may be because there is some code in the worksheet that is designed to run whenever there is a worksheet_change sub, however, I placed the entire worksheet_change code inside an if statement to try to skip it but the code still runs slowly. It fails when it gets to the worksheet_change

    I cant attach the file, because it's more than 1MB, but would anyone know if there is a way to deactivate a "Private Sub_Worksheet Change" sub so that it does not run, and then activate it back up again?

    thanks, I really appreciate the help.

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: VBA Code is running really slowly

    Maybe something along the lines of:

    Please Login or Register  to view this content.
    This will prevent the workbook from calculating formula outputs or running additional subs while the macro is running.


    Edit: I found an additional solution on the interwebs.

    Delcare a global variable at the top of your module, and set it to on while the macro runs. Worksheet_Change event can see if it's on, and stop if it is.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    and then worksheet_change macro:
    Please Login or Register  to view this content.
    Last edited by daffodil11; 06-05-2014 at 05:20 PM.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    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: VBA Code is running really slowly

    Hi,

    Try incorporating an initial Application.EnableEvents = False, and end with the corollary Application.EnableEvents = True
    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.

  4. #4
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: VBA Code is running really slowly

    Thanks everyone.

    both methods do help a bit, but I think that the problem is also that I'm just trying to copy and paste too much information in one go, and excel is have trouble to graphically display all the information.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA Code is running really slowly


    Hi,

    faster with ScreenUpdating to False !

  6. #6
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: VBA Code is running really slowly

    Hmmm, its still taking a long time to run, do you have any other ideas? the problem seems to be that Im copying anf pasting some really long formulas. Evenwhen I try to do a manual copy and paste, the sheet does not update.

  7. #7
    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: VBA Code is running really slowly

    Hi,

    Maybe it's time to upload the workbook (cut down as much as necessary in order to upload) so that we can see the request in context.

    One technique I often use to identify the problem area is to step through the macro with the F8 key and see how long the cursor takes to become live and postion itself on the next code line. Usually of course it's instantaneous so doing this you should find which is the line that is taking the time.

    You can effectively 'deactivate' any procedure by using a variable and testing the variable as the first line of the procedure. So assuming you want to control the procedure from say the A1 value in Sheet1 then put say the word 'Stop' in A1 and in the procedure

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: VBA Code is running really slowly

    Hi Richard,

    The code always fails when it gets to the line "ActiveSheet.paste". I think that the reason is because there is just too much data for excel to deal with. the file is about 8 MBs and has several tabs, all of which depend on the tab I'm working on. infact, even when I try to do manually what the code does automatically, excel still runs into trouble when it tries to paste the information.

    I'll try to trim down the work file, but its a bit difficult to get it down to 1MB. I'll try to post the file in two pieces with the relevant information.

    thanks for the help.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: VBA Code is running really slowly

    Showing the code would certainly help I am sure.
    Show where the code is, Regular Module, Sheet Module or This Workbook Module.

  10. #10
    Registered User
    Join Date
    03-13-2014
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: VBA Code is running really slowly

    Hi everyone,

    I think I figured out the problem. the issue is the conditional formating, which apparently when it gets copied, makes excel really slow. I changed the code from "activesheet.paste" to "Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone", and that seems to make things run much faster.

    thanks for all the help!

+ 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. Code running really slowly
    By The Phil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-03-2014, 08:42 PM
  2. [SOLVED] Code running slowly
    By Sweepin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-30-2013, 09:15 PM
  3. [SOLVED] Code running slowly any way of speeding up processing time
    By boomboomblock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2013, 09:09 AM
  4. Code running slowly
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-01-2011, 11:08 AM
  5. [SOLVED] Paste code running extremely slowly...
    By KR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2005, 11:05 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