从模块调用 UserForm_Initialize [英] Call UserForm_Initialize from Module
问题描述
我正在尝试从模块调用 Private Sub UserForm_Initialize.
I'm trying to call Private Sub UserForm_Initialize from a module.
UserForm_Initialize 按预期运行.这两种方法(在 Module1 中运行)都无法执行:
UserForm_Initialize runs as it is supposed to. Both these methods (running in Module1) fail to execute it:
这个
Sub Change()
Call UserForm_Initialize
End Sub
返回编译错误:子或函数未定义."
returns "Compile error: Sub or Function not defined."
这个:
Sub Change()
UserForm.Show
End Sub
返回运行时错误‘424’:需要对象.
returns "Run-time error '424': Object required.
这是我的用户表单代码:
Here's my Userform code:
推荐答案
您不应该从 UserForm 外部运行 UserForm_Initialize
.UserForm_Initialize
在实例化用户窗体时被调用.
You shouldn't be running UserForm_Initialize
from outside the UserForm. UserForm_Initialize
gets called when the UserForm is instantiated.
相反,将您想要的命令放入用户窗体代码模块中的公共过程中:
Instead, put the commands you want into a public procedure in the UserForm code module:
Public Sub ResetForm
' put stuff in here
End Sub
然后,如果您需要重置用户窗体(假设它名为 UserForm1
),请从常规模块中以这种方式调用代码:
Then if you need to reset the UserForm (let's assume it's named UserForm1
), call the code this way from a regular module:
Sub Change()
UserForm1.ResetForm
End Sub
这篇关于从模块调用 UserForm_Initialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!