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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Collating Copies

When you print multiple copies of worksheets that require more than one page each, you'll probably want those copies ...

Discover More

Quicker Multiple Replace Operations

Need to replace a lot of the same characters very, very quickly? Here's a great way to do the replacement.

Discover More

Office 365 and Excel Versions

What is Office 365 and how does it relate to the version of Excel you have? This tip provides what you need to understand ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Getting User Input in a Dialog Box

Want to get some input from the users of your workbooks? You can do it by using the InputBox function in a macro.

Discover More

Adjusting a Path Based on System and User

It is not uncommon to set variables in a macro based on other values, such as time or date. You could also set variables ...

Discover More

Workbook Events

You can create macros that run whenever Excel detects a certain event happening within an entire workbook. This tip ...

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. Maximum image size is 6Mpixels. 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 six more than 6?

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.