Macro, while Running, Stops Excel from Responding

by Allen Wyatt
(last updated December 20, 2014)

11

When Dave writes a macro that must process a lot of information in a For...Next loop, he always uses the status bar to indicate progress. Typically, he'll have a message displayed there such as "Processing row X of Y." When "Y" is a large value (the top end of the For...Next loop), Excel often stops updating the status bar and displays something like "Not Responding" in Excel's title bar. The macro is still running, though, and when it completes, Excel starts responding and everything works like it should. Dave wonders how he can make Excel stop behaving in this manner and, instead, display the status bar updates like he wants.

This behavior seems to occur when it appears to Windows that Excel has stopped responding. (It is Windows, after all, that is responsible for what appears in a program's title bar.) I was able to reproduce the behavior quite easily if I have multiple programs open in Windows and I click to another program window while Excel is chunking through a long macro. Essentially, if Excel is busy performing the macro or it cannot keep up in asking Windows to update the status bar, then it does, indeed appear like Excel has stopped responding.

There are two possible approaches that can be used. First, you could use the DoEvents command within the loop. Normally this is used to instruct the macro to pay attention to anything that is in the events queue, such as when someone presses the keyboard. It is equivalent to forcing Excel to "look up" from the macro on which it is working and to communicate with Windows. This would, necessarily, let Windows know that Excel really is responsive and allow the updates to the status bar to occur. You could even put the command right after updating the status bar:

Application.StatusBar = "Processing row " & X & " of " & Y
DoEvents

The other thing to try is to simply not update the status bar as much. If the upper end of your loop is very large, then it might be better to calculate a percentage and update the status bar when some portion of the total is completed—perhaps every 5% or 10% of the total. This would mean the status bar only needs to be updated 10 or 20 times during the loop, rather than hundreds or thousands of times. This may mean that Windows can keep up with the requests to update the status bar and, as a bonus, your macro may run faster because it doesn't have to update the status bar as often.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13341) applies to Microsoft Excel 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

An Unruly Ruler

Does the ruler in Word seem to have a mind of its own? If you have problems understanding why the ruler behaves as it does, ...

Discover More

Batch Template Changes

Changing the template associated with a couple of documents is easy, but what if a whole directory needs to be changed? These ...

Discover More

Booklet Printing in Word

Need to create a booklet with Word? Depending on your version, it could be as easy as changing how you print your final ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Easily Changing the Default Drive and Directory

Need a quick way to change the default drive and directory in a macro you are writing? Here's the commands to do it and a ...

Discover More

Setting Column Width in a Macro

Does your macro need to change the width of some columns in a worksheet? Here's how to do it.

Discover More

Quickly Dumping Array Contents

Variable arrays are used quite often in macros. If you use an array once in your macro and then need to reuse it for ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five less than 5?

2016-10-12 06:31:31

Jatin

Thanks buddy .. This "DoEvents" really helped me a lot :)


2015-11-13 05:18:25

Hans Hallebeek

Hi Allen,
True, I was able 'trigger' it to continue by adding DoEvents before the next loop
This removed the "Not Responding" which is Window's way of telling you I don't see anything happening, a nuisance but... my solution helped.


2015-11-13 04:02:07

stefanos

I face exactly the same problem in excel 2016 with windows 10, while with excel 2007 and windows XP no problem


2015-01-09 04:30:16

balthamossa2b

This is what I hate most about migrating from XP to 7: VBA hanging only happens in the latter, not the former. No matter which version of Excel, a lot of iterations in a For/do while = freeze.

DoEvents is the way to go, but unfortunately it adds A LOT of execution time. I generally tend to execute it in 1% of my iterations (through some sort of IF implementation).

If only this didn't happen, I would be more proficient at Project Euler...


2014-12-30 16:40:57

Peter

Tim
23/10 = 2.3 but 2310 returns an error.

Sub t()
Dim i As Double
i = 23 / 10
MsgBox i

i = 23 10
MsgBox i
End Sub

Works for me


2014-12-29 02:55:26

Dave

My thanks to all of you who contributed to the solution to my question.

The status bar update was not limited to large files. I had a process where the update stopped at about 220 of 4000 rows. I assume that the coordinate geometry processing being done by that particular macro became a little too much!

However, the addition of the "DoEvents" line of code appears to have cleared things up nicely, with no apparent deterioration in the time taken to run the process. That is one to be remembered!

Again, my thanks to all.
Dave


2014-12-23 15:31:07

David de Jongh

You can use the INT function for the integer part of the result: =INT(23/10) resolves to 2.


2014-12-23 08:59:29

Glenn Case

Tim:

The "" operator is apparently only recognized in VBA. If you open the VBA editor and enter "? 2310" into the immediate window, you will get the answer of 2 returned.


2014-12-22 15:33:00

Tim

Peter, 23/10 = 2.3 but 2310 returns an error.


2014-12-20 11:39:18

Peter Atherton

One point not addressed in the article is integer division. Say you want to show the update after each 10% is processed. You would use NumberofRows10 not NumberofRows/10. Say N was a prime number 23 then we would want it to update after processing every 2 rows 23/2 produces 2.3 while 232 returns 2. If we give this value the name lMult we can use the phrase:
If i Mod lMult = 0 Then
Application.StatusBar = msg & i
End If

Also expect a decrease in speed. I've seen an increase in time taken from 25 for a large N Value to 60 for a smaller value.


2014-12-20 07:18:09

Hans

Nive tip, but it doesn't always work, for some reason the fact that it's 'backgroud' processing makes it display the 'not reponding' messag while it's working perfectly.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.