What can be happening that slows down Excel to a crawl, sometimes for a minute or two, sometimes for hours, and its workload is spread across calls to DoEvents
?
Unfortunately, I can't post code because there seem to be no code causing the problem. It happens in different places without apparent pattern. Below there are details about my investigation.
Last week Excel on my computer started slowing down when running a macro that has been running without problems for years. I tested the same macro on 2 other computers, but they don't seem to have the same problem. It's difficult to test because it doesn't always slow down and it never happens on the same place. Sometimes it does it every minute, sometimes it works for 10 minutes and then it does it again. I tried two other computers for about 30 minutes and it never happened.
If I set breakpoints, it never slows down.
If I sprinkle prints it does, but never at the same place.
If I press Ctrl+Break the macro stops after a few minutes, the debugger shows the current line highlighted as usual, but Excel keeps using 25% CPU (that is 100% of two cores). At this point every click on Excel or on the VBA IDE is responsive, but very slow. It usually takes minutes before you see the cursor moving to the clicked spot. Sometimes if you wait a few minutes the control comes back and it's possible to press F8 or F5 to continue the execution, but I usually kill Excel if the CPU usage doesn't go down in 2-3 minutes.
During the investigation I created this function:
Sub DoEvents2()
Dim T As Single
T = Timer
DoEvents
Debug.Print Format(Timer - T, "0.000")
End Sub
and replaced the calls to DoEvents
with calls to DoEvents2
, and I see on the debug window that the time required for the DoEvents
is always a few thousands of a second, sometimes a few hundreds and once in a while, without apparent pattern, the time goes up. Often it goes up to ~90 seconds, sometimes less, a few times it lasted almost an hour, once it was still running the next day.
Here is an example of the output on the debug window after running the macro with the above defined DoEvents2
:
0.000
0.289
0.000
0.004
0.066
88.324
26.727
20.699
28.762
4.359
0.789
0.090
0.297
0.141
0.000
0.070
0.000
0.043
[...]
0.016
0.035
0.004
0.199
1.852
0.066
0.023
0.004
0.000
31.309
104.438
1.449
0.785
0.020
0.004
0.547
0.000
0.000
0.055
The macro is large and it's difficult to remove one piece without breaking it.
At first I thought the problem was with a form, but I removed all the forms and the problems is still there.
Then I tried working on volatile functions: I removed all the volatile functions and the problem is still there. Plus, it doesn't seem to be triggered by changes to Application.CalculationMode
or Application.EnableEvents
.
I checked if there are globals that could trigger long running garbage collection, but I didn't find anything. Plus, I don't see how garbage collection could run 12 hours (I left it running once the whole night and it was still there in the morning).
Sometimes I click on a button that runs a macro that uses JsonConverter, regular expressions, http requests, forms, volatile functions, etc., it runs for a minute or two as expected, and has no problems. Then I click on another button that runs 10 lines of code, and it's done in a few hundreds of a second. I click the same button again and again, and after a few times Excel hangs. I don't know where it hangs because it never hangs at the same line.
The problem started last week while Windows updates were going on, but I don't know if it's related. Since then I've been working full time chasing this problem without success.
I can't post any code, because the macro has 14,000 lines, and it happens every where, wherever there is a DoEvents
, even if it's a small 5 line function. But it seems to be affected by something that happened earlier.
So, my question is, what can be happening that slows down Excel to a crawl, sometimes for a minute or two, sometimes for hours, and its workload is spread across calls to DoEvents
.
Edit
It happened again about 40 minutes ago, I decided to let it vent hoping it would stop while I was working on something else. After 30 minutes I pressed Ctrl+Break to try to stop it, after a minute or two it did stop, then I did a burst of clicks on the save button of the VBA IDE, hoping that one of them would work and after a minute or two a popup showed asking if I wanted to save the file with unfinished calculation. After a few seconds, while this popup was visible, the CPU usage went to zero. I asked to save the file without completing the calculation, it did (I can see the " - Saved" on the title bar), then the CPU went on sucking 2 cores with the IDE still showing the current line highlighted. I tried with a burst of clicks on the stop button, but didn't work (yet?).
Edit 2
I found a place in the macro where if I set a breakpoint and press F5 it works consistently, but if I remove the breakpoint it works once, then it hangs.
This is the code:
T0 = Timer
Debug.Print Application.CalculationState,
DoEvents ' Here I set the breakpoint
Debug.Print Application.CalculationState, Timer - T0
This is the output on the Immediate window after running it a few times with a breakpoint (you see the time it takes me to press F5 after seeing it stopping) and twice without the breakpoint. I am pasting a snapshot because I couldn't copy from a hanging Excel ready to be killed. While that code is executed the calculation is manual and the events are disabled. I don't think it was calculating.
Tomorrow I will try to reinstall Office.
Edit 3
Uninstalling and reinstalling Office didn't help.
But I think I found one factor that seems to reliably allow Excel to work without problems or to hang: the VPN.
If I start Excel without VPN connection, then I can work without problems. If I then start the VPN connection, the first click (and the following macro) has no problems, the second click is slow, the third one hangs and Excel needs to be killed. Disconnecting the VPN after the second click doesn't help. I tested this scenario 5-6 times, always with the same result.
I can't think of any reason why Excel would be affected by the VPN connection. The only addins installed are the ones I am struggling with, they are on the local drive, no 3rd party addins installed. My macros do not access any network drive. There is one class with one member Req As New MSXML2.XMLHTTP60
which is never used during my tests.
Edit 4
Nope, nothing to do with the VPN. Today I'm in the office, without VPN, and the problem is still there.
After reinstalling Office I had the same settings as before.
Is it possible to reset everything and make a new clean Office installation that doesn't remember anything from its previous life?