codeomnitrix
codeomnitrix

Reputation: 4249

deactivate Excel VBA userform

I am having some macro in Excel vba and in that I am performing some functions on the excel sheets which takes around 30 seconds to complete. So I want to show a user form with a progress bar during that span of time.

I tried using userform.show in very start of the function and userform.hide at the end but I found that No action can be performed in background.

So just want to know if there is any turn around to let the processing be done in the background while the form is being displayed.

Many thanks :)

Private Sub CommandButton1_Click()
    '--------------Initialize the global variables----------------

   UserForm1.Show



    nameOfSheet2 = "Resource Level view"
    nameOfSheet3 = "Billable Hours"
    nameOfSheet4 = "Utilization"
    '-------------------------------------------------------------
    Dim lastRow, projectTime, nonProjectTime, leaveAndOther
    Dim loopCounter, resourceCounter
    lastRow = 0
    projectTime = 0
    nonProjectTime = 0
    leaveAndOther = 0
    resourceCounter = 2
    Set workbook1 = Workbooks.Open(File1.Value)
    Sheet3Creation
    Sheet2Creation
    Sheet4Creation
    UserForm1.Hide

End Sub

Upvotes: 2

Views: 12267

Answers (2)

Damien Dambre
Damien Dambre

Reputation: 55

Modeless/vbModeless didn't work for me when I wanted to keep seeing a userform, but have the worksheet activated after I clicked on something on the userform.

AppActivate Application.Caption solved the issue :

  • It leaves the userform on the screen.
  • But it reactivates the sheet you see on the screen.

I understand Modeless/vbModeless gives you the possibility to work on the sheet or on the userform, but it does not reactivate the sheet.

AppActivate Application.Caption does that.

Upvotes: 0

bonCodigo
bonCodigo

Reputation: 14361

The usage of Progress Bar is to show the progress of currently running code. And I wouldn't know if anyone want to do anything with the sheet while the code is running...

Anyway if you want to interact with the sheet while Form is displaying you may try to add the following code:

 UserForm.Show vbvModeless

And to update a Modeless form you must add DoEvents within your subroutine.

When you want to close the form at the end, do this:

 UserForm.Unload

Here is what I would do:

Click a button to run your macro

Private Sub Button1_Click()
   Call userform.show vbMmodeless
End Sub

Private Sub UserForm_activate()
    Call Main '-- your macro name
End Sub 

Sub Main()
'-- your code
DoEvents '-- to update the form *** important

useroform.Unload
End Sub

After OP showed his code:

Why do we need a progress bar?

When macros take a long time to run, people get nervous. Did it crash? How much longer will it take? Do I have time to run to the bathroom? Relax...

Upvotes: 6

Related Questions