简体   繁体   English

Excel VBA-运行时错误'424':必需对象

[英]Excel VBA - Run-Time Error '424': Object Required

I have the following code for a Check Box (ActiveX Control) using Excel 2013: 对于使用Excel 2013的复选框(ActiveX控件),我具有以下代码:

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
    CheckBox2.Value = False
Else
    CheckBox2.Value = True
End If
End Sub

Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
    CheckBox1.Value = False
Else
    CheckBox1.Value = True
End If
End Sub

What it does is simply uncheck Box 2 if I check Box 1, and vice versa. 它的作用是,如果我选中方框1,则取消选中方框2,反之亦然。 But if I use the above code for a Check Box (Form Control), I get a "Run-time error '424': Object required" error message. 但是,如果我将上述代码用于复选框(窗体控件),则会收到“运行时错误'424':所需对象”错误消息。 Does anyone know of a solution? 有人知道解决方案吗?

UPDATE: The same code above that I tried using in a file that I got from Bytes somehow worked. 更新:上面我尝试从Bytes获得的文件中尝试使用的相同代码,以某种方式起作用。 Since I'm a newbie in VBA, I think I'm gonna have to sit down and study how Excel, Macros & VBA work together. 因为我是VBA的新手,所以我认为我必须坐下来研究Excel,Macros和VBA如何协同工作。 Once I find out the source of the problem (that technically I created myself), I'll post an answer here as to how I figured it out. 一旦找到问题的根源(从技术上讲就是我自己创建的),我将在此处发布有关如何解决问题的答案。 Thanks to everyone that posted comments & replies. 感谢所有发表评论和回复的人。 I really appreciate it! 我真的很感激!

You refer to a Form control by name or index into the Checkboxes collection: 您通过名称或索引将复选框控件引用到Checkboxes集合中:

Activesheet.CheckBoxes(1).Value

for example. 例如。

You use the incorrect Event listener. 您使用了不正确的事件侦听器。 Try this 尝试这个

Private Sub CheckBox1_Change()
    If CheckBox1 = True Then
        CheckBox2 = False
    Else
        CheckBox2 = True
    End If        
End Sub

Here is the list event listeners for checkbox 这是复选框的列表事件侦听器 在此处输入图片说明

There's a UX (user experience) issue here. 此处存在UX(用户体验)问题。

  • Checkboxes are for when you need the user to pick one or more values. 复选框用于当您需要用户选择一个或多个值时。
  • OptionButtons are for when you need the user to pick one of many mutually exclusive values. OptionButton用于需要用户选择许多互斥值之一的情况。

So what you need is OptionButtons (ActiveX controls) - make sure they have the same GroupName , and you'll get the exact same behavior you're trying to achieve, without writing a single line of code: 因此,您需要的是OptionButtons(ActiveX控件)-确保它们具有相同的GroupName ,并且无需编写任何代码即可获得与您尝试实现的行为完全相同的行为:

选项按钮(ActiveX控件)

And then in your VBA code you can access them all by their name (eg OptionButton1 ) to verify their value. 然后,在您的VBA代码中,您可以按名称访问它们(例如OptionButton1 )以验证其值。


Form controls vs ActiveX controls 表单控件与ActiveX控件

"Form Controls" are just shapes that you can assign to a macro (eg do something when the user clicks it) - you can't access them and their properties in code like you would with ActiveX controls. “表单控件”只是可以分配给宏的形状(例如,当用户单击它时执行某些操作)-您无法像使用ActiveX控件那样在代码中访问它们及其属性。

With ActiveX controls you can access them as if they were global object variables, and do what you did: 使用ActiveX控件,您可以像访问全局对象变量一样访问它们,并执行以下操作:

CheckBox2.Value = True

The biggest hint you get, is IntelliSense : with ActiveX controls, when you type the dot ( . ) after the control's name, you get a dropdown in the editor, telling you that the VBE understands what you're talking about; 您得到的最大提示是IntelliSense :对于ActiveX控件,当在控件名称后键入点( . )时,您会在编辑器中看到一个下拉菜单,告诉您VBE了解您在说什么。 when you do the same with a "form control", you don't get that dropdown, and the VBE does NOT understand what you're talking about - and if you persist and run that code, you'll run into run-time errors like you did. 当您对“表单控件”执行相同操作时,您不会得到该下拉列表,并且VBE无法理解您在说什么-如果您坚持并运行该代码,则会遇到运行时问题像您一样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM