且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

停用Excel VBA用户窗体

更新时间:2023-11-30 15:13:40

进度栏的用法是显示当前正在运行的代码的进度。而且我不知道有没有人想要在代码运行时对该表进行任何操作...



无论如何,如果要与表单交互,而Form是显示您可能会尝试添加以下代码:

  UserForm.Show vbvModeless 
/ pre>

要更新 Modeless 表单,您必须添加 DoEvents



当您想关闭窗体时,请执行以下操作:

  UserForm.Unload 

这里是我会做的:



点击一个按钮来运行宏

  Private Sub Button1_Click()
调用userform.show vbMmodeless
End Sub

Private Sub UserForm_activate()
调用Main' - 你的宏名
End Sub

Sub Main()
' - 你的代码
DoEvents' - 更新表单*** important

useroform。卸载
End Sub






OP显示他的代码后:



为什么需要一个进度条?




当宏需要很长时间运行时,人们会紧张。它崩溃了吗需要多长时间?我有时间跑到浴室吗?放松...





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

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...