vba 如何加载每个用户窗体而不必单独调用 .Show?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13965608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I load every UserForm without having to call .Show individually?
提问by Daniel
I wanted to figure out how you could load every UserForm without having to call Userform1.Show
UserForm2.Show
etc. This was inspired by comments on this answer: Excel VBA UserForm_Initialize() in Module.
我想弄清楚如何在无需调用Userform1.Show
UserForm2.Show
等的情况下加载每个用户窗体。这是受到对这个答案的评论的启发:Excel VBA UserForm_Initialize() in Module。
I found this method suggested in a few places:
我在几个地方发现了这种方法:
Sub OpenAllUserForms()
Dim uf As UserForm
For Each uf In UserForms
uf.Show
Next
End Sub
However, no Userforms display regardless of how many are attached to the workbook. When I stepped through the code I determined that the UserForms collection is empty!
但是,无论附加到工作簿的用户表单有多少,都不会显示。当我逐步执行代码时,我确定 UserForms 集合是空的!
How can I load each Userform without having to explicitly show each one?
如何加载每个用户表单而不必显式显示每个用户表单?
回答by Daniel
According to this page: UserForm Object
根据此页面:用户窗体对象
The UserForms collection is a collection whose elements represent each loaded UserForm in an application.
UserForms 集合是一个集合,其元素代表应用程序中每个加载的用户窗体。
Since your Userforms are not loaded they do not appear in the collection. This means you'll need to load the forms a different way. In order obtain the name of each UserForm you will need to allow permission for code to access the Visual Basic Project. Otherwise, the name of the Userform will need to be provided by you.
由于您的用户表单未加载,因此它们不会出现在集合中。这意味着您需要以不同的方式加载表单。为了获得每个用户窗体的名称,您需要允许代码访问 Visual Basic 项目。否则,您需要提供用户表单的名称。
The following will open every UserForm in the current VBProject.
下面将打开当前 VBProject 中的每个用户窗体。
Sub OpenAllUserForms()
Dim VBComp As Object
For Each VBComp In Application.VBE.ActiveVBProject.VBComponents
If VBComp.Type = 3 Then '3 = vbext_ct_MSForm
VBA.UserForms.Add(VBComp.Name).Show
End If
Next
End Sub
This works because each UserForm is listed in the VBProject as a VBComponent. Once the code determine which components are UserForms, it adds the Userform to the collection and displays it. If you omit the .Show
the form will still run the Initialize event, but then immediately go out of scope and vanish.
这是有效的,因为每个用户窗体都作为 VBComponent 列在 VBProject 中。一旦代码确定哪些组件是用户窗体,它就会将用户窗体添加到集合中并显示它。如果省略该.Show
表单,该表单仍将运行 Initialize 事件,但随后会立即超出范围并消失。
回答by 4 Leave Cover
I've solved the existing problem by applying the line below
我已经通过应用下面的行解决了现有的问题
In Module:
UserForms(Name).Userform_Initialize
在模块中:
UserForms(Name).Userform_Initialize
Description:
This will initialize the current UserForm.
描述:
这将初始化当前用户窗体。
Example:
If result = True Then
UserForms(Name).Userform_Initialize
示例:
如果 result = True Then
UserForms(Name).Userform_Initialize
This means that when the result is true, the form will change to the initial state.
这意味着当结果为真时,表单将更改为初始状态。
P.S Pardon my bad English
PS请原谅我的英语不好