gpt4 book ai didi

excel - 在Excel VBA中访问CheckBox的值时如何解决运行时错误424?

转载 作者:行者123 更新时间:2023-12-03 08:54:37 26 4
gpt4 key购买 nike

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

示例:

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

现在我的问题是,它一直给我运行时错误424。
谁能帮我这个?
非常感谢您的帮助。
谢谢。

更新:
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).Activate之后停止。
非常感谢您的帮助。
谢谢。

最佳答案

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

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

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

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

(根据评论中的要求)更新:这是我所拥有的:
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(从“开发人员”选项卡上的“宏”对话框中)可以使我:

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

关于excel - 在Excel VBA中访问CheckBox的值时如何解决运行时错误424?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com