VBA Takes Over 1 Hour To Run

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
331
Office Version
  1. 365
Platform
  1. Windows
I have 1 macro that calls several other macros. I am calling instead of running each code separately because I’d like to run everything with the push of 1 button. I have the call set up like this:

Code:
  Sub RUN_ALL()

   

      OptimizeCode_Begin

      01_IMPORT

      02_FIND_DATE

      03_ROLE

      04_NEW

      05_ADD

      06_STATUS_FIX

      07_LEVEL

      08_ FORMATTING

      09_E2

      10_E2_UPDATES

      11_ FORMATTING2

      12_LAST

      13_HIDE

      Save_EXCEL_Files

      OptimizeCode_End

      Complete_Message


  End Sub

When running this macro the time to complete is almost 1 hour + 30 minutes. During this time my computer freezes and becomes very slow. However, if I run each macro 1 by 1 OR run this single call macro by using F8 (step into) the entire process takes no longer than 10 minutes (still a long time, but a whole lot better than 90 minutes).

Any idea on why pne way or running the macros is faster than the other?

Any code that I can add to make things faster?

I have to run this process 3 times a day so having this move faster is very important.


I am using an OptimizeCode_Begin and OptimizeCode_End, but it doesn’t seem to help at all.

Code:
  Sub OptimizeCode_Begin()
      

      Application.ScreenUpdating = False

      Application.ErrorCheckingOptions.BackgroundChecking = False

      
      EventState = Application.EnableEvents
      Application.EnableEvents = False
      
      PageBreakState = ActiveSheet.DisplayPageBreaks
      ActiveSheet.DisplayPageBreaks = False
      
  End Sub



Code:
  Sub OptimizeCode_End()
      
      ActiveSheet.DisplayPageBreaks = PageBreakState
      Application.EnableEvents = EventState
      Application.ScreenUpdating = True
      Range("A1").Select
  End Sub
 
Hi Nanogirl

If you hit ctrl-end it takes you to what Excel thinks is the last used cell in the sheet. if it's not, say someone copied 100k rows and then deleted the data, Excel still thinks they're used. So if you're sorting a column and it thinks there's data to the last possible row it can take a long time.

To fix, go to the lowest cell with data in that you can find. Then ctrl-shift down arrow to select to the bottom of the sheet. Delete the rows. Do the same with columns if it's a problem. Then select cell A1 and save. this forces XL to reset the last known cell. I think in newer versions selecting A1 may not be needed but I'm not sure.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
How are you removing duplicates? the fastest way to do that is mark each duplicated row in a spare column at end, then sort on this column so they are all at the bottom then delete the entire range in one go. If you are deleting line by line that will be sloooow.

Code:
    ActiveSheet.Range("$A:$Z").RemoveDuplicates Columns:=1, Header:=xlYes
 
Upvote 0
That will be slow, because every time a row is deleted EXCEL has to move every row below that up one row and it does that for every duplicate, I suggest filtering on duplicates and doing as i suggested above
 
Last edited:
Upvote 0
That will be slow, because every time a row is deleted EXCEL has to move every row below that up one row and it does that for every duplicate, I suggest filtering on duplicates and doing as i suggested above

I don't believe that the 'RemoveDuplicates' function loops row by row. That function is pretty fast.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top