在Excel VBA中访问CheckBox的值时如何解决运行时错误424? [英] How to fix run time error 424 when accessing the value of a CheckBox in Excel VBA?

查看:306
本文介绍了在Excel VBA中访问CheckBox的值时如何解决运行时错误424?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是问题所在:
在我的用户表单中,我有2个复选框。我希望能够使用复选框的值来执行特定的工作。

Okay so here is the problem: In my user form, I have 2 checkboxes. I want to be able to use the value of the checkbox to perform a certain job.

示例:

Sub main()
    UserForm1.Show
    If UserForm1.CheckBox1.Value=True Then
        MsgBox("Awesome")
    End If
End Sub



< hr>

现在我的问题是,它一直给我带来运行时错误424。
有人可以帮助我吗?
非常感谢您的帮助。
谢谢。


Now my problem with this is that it keeps giving me run time error 424. Can anyone help me with this? Your help is greatly appreciated. Thank you.

更新:

Sub main()
    UserForm1.Show
    If UserForm1.CheckBox1.Value=True Then
        Worksheets(1).Activate
        If UserForm1.CheckBox1.Value=True Then
            MsgBox("Awesome")
        End If
    End If
End Sub

现在,它在 worksheets(1)之后停止。激活
非常感谢您的帮助。
谢谢。

Okay now it stops after worksheets(1).Activate. Your help is greatly appreciated. Thank you.

推荐答案

解决方案:该代码对我有用:

Sub main()
    If UserForm1.CheckBox1.Value = True Then
        MsgBox "Checkbox is checked"
    End If
End Sub

说明:错误出现是因为您没有指定 CheckBox1 属于哪个对象(在本例中为哪种形式)。因此,我在 If 语句中添加了 UserForm1。。其次, CheckBox1.Value 是一个布尔型属性,即,选中该值将为 True ,而不是 1

Explanation: The error appears because you did not specify which object (in this case: which form) CheckBox1 belongs to. Hence I added the UserForm1. in the If statement. Secondly, CheckBox1.Value is a boolean property, i.e. the value will be True when checked, not 1.

其他信息:请注意,运行 If UserForm1.Show 之后的子句(就像您在示例中所做的一样)将永远无法工作,以防您打算选择 .Show 命令。甚至在没有时间选择复选框之前,将显示该表单并运行 If 子句。所以我的答案中的代码应该转到另一个Sub,例如单击窗体中的按钮时运行一次(您是否在窗体上具有某种确定或关闭按钮?如果是,请在宏编辑器中双击该按钮并在其中添加代码)。让我知道您是否需要更多上下文。

Additional information: Please note that running the If clause just after UserForm1.Show (like you did in your example) will never work in case you intend to select the checkboxes after the .Show command. The form will be shown and the If clause be run before you even had the time to select the checkbox. So the code in my answer should go to another Sub, e.g. the one run when you click a button in your form (do you have some sort of "OK" or "Close" button on it? If yes, double click the button in the macro editor and add the code there). Let me know if you need more context.

更新(根据评论的要求):这是我拥有的:

Update (as requested in the comments): Here's what I have:

Sub a()
    ' This launches the form
    ' I added this to a normal Module in the
    ' VBA editor
    UserForm1.Show
End Sub

Private Sub CommandButton1_Click()
    ' This is what is executed when clicking
    ' the "OK" button
    ' To add this code, add a button to your
    ' form, double click it and paste this code
    If UserForm1.CheckBox1.Value = True Then
        Worksheets(1).Activate
        MsgBox "Awesome"
    End If
    ' Update 2: Close form but keep
    ' Checkbox1.Value available
    Userform1.Hide
End Sub

运行 a (摘自宏 开发人员标签上的对话框)给我:

Running a (from the "Macros" dialogue on the "Developer" tab) gives me:

选中复选框并单击确定将返回以下内容:

Selecting the checkbox and clicking OK returns this:

这篇关于在Excel VBA中访问CheckBox的值时如何解决运行时错误424?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆