0 votes
in VBA by Beginner (8 points)

Hi,

I followed the tutorial on wellsr.com to show a progress bar while processing data in an Excel sheet.  Using Excel 2016 on Windows 10 Professional.

This is the link to the tutorial: https://wellsr.com/vba/2017/excel/beautiful-vba-progress-bar-with-step-by-step-instructions/

All is fine in the beginning (I have a quite resource intensive algo running through a quite busy Excel sheet) but at a certain moment the progress bar stalls and even the spreadsheet becomes totally black until the regular macro is finished.

I've added the ufProgress.Repaint method after the update of the progress bar values, as suggested in the tutorial.  However, no improvement.

Then I've added a delay of 10ms using the Sleep method but that also doesn't bring any improvement.

What could cause this behaviour and how to resolve it?

PS: in the meantime I'm using the Application.Statusbar method to "mimic" the progress bar but it would be much nicer if the progress bar would work as explained/expected in the tutorial.

Any hints welcome!

Best,

--Geert

1 Answer

0 votes
by Super Expert (3.2k points)
selected by
 
Best answer

Make sure you're including the VBA DoEvents keyword in your macro loop. I suspect you already are, but this is the low-hanging fruit to check before going into these macro-rewrite alternatives. VBA is a slow programming language and it's not uncommon for resource intensive algorithms to freeze Excel while they finishes processing. Here are a couple things you can try if you're still having performance issues:

  1. Make sure your macro is optimized for speed by disabling application-level time-wasters like ScreenUpdating and Calculations.
  2. Try to avoid calling worksheets and cells throughout your macro. Instead define these worksheets and ranges with variables. Here's what I mean by defining worksheets as variables, but you can do the same with ranges:
    1. Dim ws1 As Worksheet
      Set ws1 = Worksheets("Sheet1")
      ws1.Range("A1") = "hello"
  3. If you're updating a lot of values in your spreadsheet as your algorithm runs, you may be able to get a big performance improvement by storing your results in Variant arrays while your algorithm runs and then entering them into your final spreadsheet at the very end of your macro once you're outside your loop.
by Beginner (8 points)
+1

The DoEvents did the trick.  I missed that completely while implementing your example...

Thanks for the swift response!

Best,

--Geert

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...