Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a code running in VBA while a form window is open?

In Excel VBA, I want to push a button, run some code, and while the code is running have a form window appear saying something like "Code is running, please wait until finished. Click stop button to terminate code."

But every time I .Show a form the code stops, waiting for the form window to be closed before continuing. Is there a way to force the code to continue to run while the form window is open? Thanks!

Answered: Add (False) at the end of .Show or change the form's ShowModal property to False. Then add Application.Wait (Now + TimeValue("0:00:01")) just after fmRunning.Show(False). Or insert "DoEvents" instead of the Application.Wait Thanks all!!

like image 300
Steven Avatar asked Feb 18 '16 14:02

Steven


2 Answers

You need to set the userform to be modeless. This can be done two ways:

  • either by selecting the userform and changing the ShowModal property to False
  • or by opening the userform with setting the modal property to 0 (vbModeless)
Userform.Show vbModeless

See MSDN for more info.

like image 63
vacip Avatar answered Sep 17 '22 23:09

vacip


Using only Userform.Show vbModeless can cause blank form. You should also use the command Userform.Repaint. This will refresh the info on the form. Example:

Userform.Show vbModeless
Userform.Caption = "Some text"   'change the Caption text
Userform.Repaint   'refresh changes

This way you should see the changes

like image 26
elano7 Avatar answered Sep 21 '22 23:09

elano7