vba Userform.Show on a form button 无法识别 userform,出现错误 424
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27490359/
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
Userform.Show on a form button will not recognize userform, getting Error 424
提问by Tyler Medina
I know very little about VBA, but I'm trying to design a userform for an excel workbook. The idea is click the button, bring up the userform, enter info, hit OK, and your info is formatted correctly and inserted into the worksheet.
我对 VBA 知之甚少,但我正在尝试为 Excel 工作簿设计用户表单。这个想法是单击按钮,调出用户表单,输入信息,点击确定,然后您的信息格式正确并插入到工作表中。
I have 3 userforms that all work fine, but any macro I create that references one of them just does not recognize that that particular userform exists.
我有 3 个用户表单,它们都可以正常工作,但是我创建的任何引用其中一个的宏都无法识别该特定用户表单的存在。
The code I'm having a problem with is pretty straightforward:
我遇到问题的代码非常简单:
Private Sub LiquidFormButton_Click()
LiquidEntryUserform.Show
End Sub
Edit (Update): So I tried making a new userform, with a different name. I copied and pasted all of the controls from the object over to the new userform, changed the name of the macro to bring up the userform, and voila, it works. However, now the userform itself doesn't do anything because none of the controls actually have any codes behind them telling them what to do. That's fine, I'll just copy over the codes from the broken form and BOOM now it doesn't work. Soooo something in the very very simple coding within the userform itself is preventing it from being shown, even though the new userform AND the broken one both, in fact, do everything else they need to do besides show up. I'll post the full userform code up later on after some dabbling. Thank you!
编辑(更新):所以我尝试使用不同的名称创建一个新的用户表单。我将对象中的所有控件复制并粘贴到新的用户窗体上,更改了宏的名称以显示用户窗体,瞧,它可以工作了。但是,现在用户窗体本身不做任何事情,因为实际上没有任何控件背后有任何代码告诉它们该做什么。没关系,我只是从损坏的表单和 BOOM 中复制代码,现在它不起作用了。用户表单本身非常非常简单的编码中的某些东西阻止了它的显示,即使新的用户表单和损坏的用户表单实际上都做了除了显示之外他们需要做的所有事情。稍后我会在一些涉足之后发布完整的用户表单代码。谢谢!
采纳答案by rene
You should 'instantiate' the form like so
你应该像这样“实例化”表单
Private Sub LiquidFormButton_Click()
Dim liquid as LiquidEntryUserform ' define a liquid var of the correct type
Set liquid = new LiquidEntryUserform ' create the Form
liquid.Show 'show it
' here you can still access variables
' on the form
If liquid.TextBox1.Text = "700" Then
'do things
End if
End Sub
My project looks like this:
我的项目是这样的:
You can use the Object Browser (View|Object Browser or hit F2) to find the Forms and Classes you have in your project:
您可以使用对象浏览器(查看|对象浏览器或点击F2)来查找您项目中的表单和类:
回答by Steve
I had a similar experience after making some changes to a UserForm. I noticed something was actually working immediately prior to the 424 error occurring. Using F8 to step thru my code, it turned out I was asking a control to be configured - that I had deleted!!
I was using this code in the MS XL Objects worksheet (the button is on the associated worksheet)...
在对用户窗体进行一些更改后,我也有类似的经历。我注意到在 424 错误发生之前,某些东西实际上正在起作用。使用 F8 单步执行我的代码,结果我要求配置一个控件 - 我已经删除了!!
我在 MS XL Objects 工作表中使用了这段代码(按钮在相关的工作表上)...
Private Sub cmdTransactions_Click()
frmTransactions.Show
End Sub
to bring up the UserForm and this code in the Forms module...
在 Forms 模块中调出 UserForm 和此代码...
Private Sub UserForm_Initialize()
: : :
Row_Number = [mostrecent].Value
Cells(Row_Number + 1, 2).Select <<< this bit was happening (.Select works for me!!)
: : :
cmdReset.Enabled = False <<< a control in the UserForm
chkDeposit.Enabled = False <<< this control had been deleted!! Remarked out but un-Remarked for clarity
: : :
End Sub
Hope that might help someone.
希望这可以帮助某人。