vba 运行时错误 424 Object Required UserForm 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31352297/
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
Run-time Error 424 Object Required UserForm doesnt exist
提问by VBAJam
I'm attempting to do the things written on this site www.excel-easy.combut when I click the commandbutton(from ActiveX controls) in the worksheet just like what the website instructed, nothing happens. I tried to use a button from form controls, but it says that the error is in this
---> DinnerPlannerUserForm.Show
我正在尝试执行此网站www.excel-easy.com上写的内容,但是当我commandbutton按照网站指示单击工作表中的(来自 ActiveX 控件)时,没有任何反应。我尝试使用表单控件中的按钮,但它说错误在此 ---> DinnerPlannerUserForm.Show
My Code:
我的代码:
Sub Button2_Click()
DinnerPlannerUserForm.Show
End Sub
When I used F8, it said the error is here --> Private Sub UserForm_Initialize()
当我使用 F8 时,它说错误在这里 --> Private Sub UserForm_Initialize()
Private Sub UserForm_Initialize()
'Empty NameTextBox
NameTextBox.Value = ""
'Empty PhoneTextBox
PhoneTextBox.Value = ""
'Empty CityListBox
CityListBox.Clear
'Fill CityListBox
With CityListBox
.AddItem "San Francisco"
.AddItem "Oakland"
.AddItem "Richmond"
End With
'Empty DinnerComboBox
DinnerComboBox.Clear
'Fill DinnerComboBox
With DinnerComboBox
.AddItem "Italian"
.AddItem "Chinese"
.AddItem "Frites and Meat"
End With
'Uncheck DataCheckBoxes
DateCheckBox1.Value = False
DateCheckBox2.Value = False
DateCheckBox3.Value = False
'Set no car as default
CarOptionButton2.Value = True
'Empty MoneyTextBox
MoneyTextBox.Value = ""
'Set Focus on NameTextBox
NameTextBox.SetFocus
End Sub
回答by Bart
This error can also occur when you remove or delete a textbox from your form, but forget to remove it from a line in initialization for eg:
当您从表单中移除或删除文本框,但忘记将其从初始化行中移除时,也会发生此错误,例如:
Private Sub UserForm_Initialize()
CommandButton2.Enabled = False
TextBox4.Enabled = False 'textbox deleted from form
End sub
回答by David
I was using the same tutorial and solved the problem by changing the Initialize command:
我正在使用相同的教程并通过更改 Initialize 命令解决了问题:
It is given as
它被给出为
Private Sub UserForm_Initialize()
I named my user form (for my own purposes)
我命名了我的用户表单(出于我自己的目的)
StdTimeCalculatorForm
and changing the code to
并将代码更改为
Private Sub StdTimeCalculatorForm_Initialize()
solved the problem. Hope this helps.
解决了这个问题。希望这可以帮助。
回答by Stephanie
I'm assuming this issue has been resolved, but for anyone just now looking at it. I had this issue and it turned out to be that I had removed a ComboBox from my form, but it was still referenced in the code. Once I removed that section of the code, it worked beautifully.
我假设这个问题已经解决,但对于刚刚看到它的任何人。我遇到了这个问题,结果是我从表单中删除了一个 ComboBox,但它仍然在代码中被引用。一旦我删除了代码的那部分,它就运行得很好。
回答by Ryder88
I was able to solve these by changing
我能够通过改变来解决这些问题
Private Sub UserForm_Initialize()
to
到
Private Sub DinnerPlannerUserForm_Initialize()
See if it works
看看它是否有效
回答by John Coleman
Hard to tell based on what you have said. But -- the fact that you said using F8 indicated that the error is in Private Sub UserForm_Initialize()suggests that the userform exists and VBA knows how to find it (otherwise its initialize event wouldn't be firing when you click the form button). Hence -- it is one of the lines in the initialize sub which is the culprit. Which line specifically is flagged? I'm guessing that a simple typo in the name of one of the controls (e.g. DinnerComboBox) is the problem.
很难根据你所说的来判断。但是 - 您所说的使用 F8 表示错误的事实Private Sub UserForm_Initialize()表明该用户表单存在并且 VBA 知道如何找到它(否则当您单击表单按钮时它的初始化事件不会被触发)。因此 - 它是 initialize sub 中的行之一,这是罪魁祸首。具体标记了哪一行?我猜测其中一个控件(例如 DinnerComboBox)的名称中的一个简单的错字是问题所在。
回答by technoman23
I had this same issue. I was creating the same form in a few workbooks and using the same variable names. I pasted in my code for UserForm_Initialize. Everything looked good in the code, but I went back and double checked my variable names on the form and realized I had forgotten to name two of the text boxes on my form. My code was trying to assign values to txtMaxLines and txtAmount but I hadn't named them on the form, so to the vba it was like they didn't exist.
我有同样的问题。我在几个工作簿中创建了相同的表单并使用了相同的变量名。我粘贴了 UserForm_Initialize 的代码。代码中的一切看起来都很好,但我回去仔细检查了表单上的变量名称,发现我忘记在表单上命名两个文本框。我的代码试图为 txtMaxLines 和 txtAmount 赋值,但我没有在表单上命名它们,所以对 vba 来说它们就像不存在一样。
I hope this will help someone because if you have the same issue as I did, when it opens up the vba editor it doesn't go to the line where the error is and the error description is not helpful at all.
我希望这会对某人有所帮助,因为如果您遇到与我相同的问题,当它打开 vba 编辑器时,它不会转到错误所在的行,并且错误描述根本没有帮助。

