Macro runs more slowly each time it is run

Jehannum_2000

New Member
Joined
Sep 14, 2024
Messages
4
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
I have a .xlsm spreadsheet with a button-activated macro that runs calculations on spreadsheet data.

Each time I call the macro it takes about 0.15 seconds longer to complete, even when using the same initial data.

The slowdown becomes noticeable and obvious after a while. A calculation that previously took 0.5 seconds now takes 6.5 seconds.

If I save and re-load the spreadsheet, it goes back to its fastest speed (but again slows down on each subsequent call).

Since the re-loaded spreadsheet has exactly the same data as the saved slow version, the problem surely cannot be some unseen change in the input/output data.

Could it be because the macro is being allocated more and more memory on each use? I used Task Manager to confirm that Excel's memory allocation increases each time the macro is called.

How do I fix this?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Those are the symptons of a memory leak, EXCEL VBA is notably bad at managing memory, so you have to help it along a bit. The way to do this is to set all the declared objects to Nothing when you have finished with them and before you exit the macro. In theory you shouldn't need to do this, so I have usually tried setting things to zero in the major suspect procedures and then see if that works.
 
Upvote 0
Solution
Those are the symptons of a memory leak, EXCEL VBA is notably bad at managing memory, so you have to help it along a bit. The way to do this is to set all the declared objects to Nothing when you have finished with them and before you exit the macro. In theory you shouldn't need to do this, so I have usually tried setting things to zero in the major suspect procedures and then see if that works.
Thank you, I will try this.

One other thing that might be relevant is that the lag increase is worse for larger datasets.
 
Upvote 0
The problem is now fixed. I am not quite certain why it went away.

The speedup appeared to occur when I removed a loop from the calculation macro. I don't understand why this had such a dramatic effect (there were only around 12 iterations of the loop). It's as though there is a certain memory usage threshold above which Excel gets bad but below which things are fine.
 
Upvote 0

Forum statistics

Threads
1,221,483
Messages
6,160,095
Members
451,617
Latest member
vincenzo1

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