vba 单击按钮后未出现用户窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7891592/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:07:31  来源:igfitidea点击:

UserForm not coming up after button click

excelvba

提问by Jon

I created two new user forms for a worksheet. After I made them I added two buttons to the excel page. Then I set up a module and put in

我为工作表创建了两个新的用户表单。制作完成后,我在 Excel 页面中添加了两个按钮。然后我设置了一个模块并放入

Sub Button4_Click()
Missing.Show
End Sub

Now that I assigned the macro to the button, when I click the button I get an "object required" error. Why is this, and what can I do to make th buttons actually open the usr form if the above is not working?

现在我将宏分配给按钮,当我单击按钮时,出现“需要对象”错误。这是为什么,如果上述方法不起作用,我该怎么做才能使按钮实际打开 usr 表单?

采纳答案by Jon

Well, I finally figured out the problem. It actually had nothing to do with the button itself. Missing.Showworks perfectly fine. It was actually a problem with the form initialization; at some point I changed the initialization to

嗯,我终于找到了问题所在。它实际上与按钮本身无关。 Missing.Show工作得很好。其实是表单初始化的问题;在某些时候,我将初始化更改为

Private Sub Missing_Initialize()
    TextBox.Value = ""
End Sub

when I actually should have left it at

当我实际上应该把它留在

Private Sub UserForm2_Initialize()
    TextBox.Value = ""
End Sub

So, apparently, even though the error was always taking me to the button code, it was actually the form's initialization that was causing the fuss. Hope this helps anyone else with a similar issue.

所以,显然,即使错误总是把我带到按钮代码,它实际上是表单的初始化引起了大惊小怪。希望这可以帮助其他有类似问题的人。

回答by Fionnuala

How about:

怎么样:

Private Sub Missing_Click()
Load Missing
Missing.Show
End Sub

回答by JimmyPena

Since you are outside the form's (class) module, VBA has no idea what "Missing" refers to. You have to instantiate it first.

由于您在表单的(类)模块之外,因此 VBA 不知道“缺失”指的是什么。你必须先实例化它。

Dim frm As Missing

Set frm = New Missing

Missing.Show

And if I may add, it's none of my business but "Missing.Show" could be confusing. I would avoid the use of variables that look like keywords.

如果我可以补充,这不关我的事,但“Missing.Show”可能会令人困惑。我会避免使用看起来像关键字的变量。