停用Excel VBA用户窗体 [英] deactivate Excel VBA userform

查看:717
本文介绍了停用Excel VBA用户窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel vba中有一些宏,因为我在excel表上执行一些功能,大约需要30秒才能完成。所以我想在这段时间内显示一个进度条的用户表单。



我尝试使用 userform.show 在函数的非常开始和 userform.hide 中,但我发现在后台不能执行任何操作。



所以只是想知道是否有任何转向,以便在窗体显示时在后台完成处理。



非常感谢: )

  Private Sub CommandButton1_Click()
'--------------初始化全局变量----------------

UserForm1.Show



nameOfSheet2 =资源级别视图
nameOfSheet3 =可结算小时
nameOfSheet4 =利用率
'----------------------- --------------------------------------
Dim lastRow,projectTime,nonProjectTime,leaveAndOther
Dim loopCounter,resourceCounter
lastRow = 0
projectTime = 0
nonProj ectTime = 0
leaveAndOther = 0
resourceCounter = 2
设置工作簿1 = Workbooks.Open(File1.Value)
Sheet3创建
Sheet2创建
Sheet4创建
UserForm1.Hide

End Sub


解决方案

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



无论如何,如果要与表单交互,而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...

这篇关于停用Excel VBA用户窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆