基于COMBOBOX的对象所需的错误启用/禁用Combobox/复选框
我正在研究一个重复系列的表,一个连击链接链接到另一个Combobox和一个复选框。
第一个框有四个可选选项。
如果选择了前两个选项中的任何一个,则应禁用第二个ComboBox和复选框。
如果选择了最后两个选项中的任何一个,则第二个框和复选框都应启用。
当我第一次设置此代码时,一切都起作用。我不小心将自己扔进了一个无尽的循环中,不得不从“ Excel Recoved”工作表开始,该工作表擦洗了我所有的控件。
我重新分配控件。现在,当我打开工作簿时,我必须单击一个“必需对象”运行时错误的五十个(是的,没有笑话,五零)。当我关闭工作簿时,也会发生同样的事情。但是,当我清除所有错误时,代码会运行。使用“选项显式”给我一个“变量未定义”错误。
我无法识别任何需要定义的变量。
Sub ComboBox1_Change()
If ComboBox1.ListIndex = 2 Or ComboBox1.ListIndex = 3 Then
ComboBox6.Enabled = True
CheckBox1.Enabled = True
Else: ComboBox6.Enabled = False
CheckBox1.Enabled = False
End If
End Sub
这是一个实例。在我的项目中,我有二十五个重复一次。
I'm working on a sheet that has a repeating series of one combobox linked to one other combobox and a checkbox.
The first box has four selectable options.
If either of the first two options is selected, both the second combobox and the checkbox should be disabled.
If either of the last two options is selected, the second box and checkbox should both become enabled.
When I first set up this code, everything worked. I accidently threw myself into an endless loop and had to start with the "Excel recovered" worksheet, which scrubbed all my controls.
I re-did the controls. Now when I open the workbook, I have to click through fifty (yes, no joke, five-zero) instances of an "object required" run-time error. The same thing happens when I close the workbook. But when I clear all the errors, the code runs. Using "Option Explicit" gives me a "variable not defined" error.
I can't identify any variables which need defining.
Sub ComboBox1_Change()
If ComboBox1.ListIndex = 2 Or ComboBox1.ListIndex = 3 Then
ComboBox6.Enabled = True
CheckBox1.Enabled = True
Else: ComboBox6.Enabled = False
CheckBox1.Enabled = False
End If
End Sub
This is for one instance. I have twenty-five of these repeated, one after the other, in my project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅供参考,您可以使用命名方案避免大部分复制的代码,该方案允许您将3个控件的集合关联:例如,请参见下文。然后,您可以使用一个单个过程,将第一个组合作为参数,可以执行检查。
例如(您可能会根据您的控件的实际代表提出更多的名称):
那么您的代码看起来像这样:
FYI you can avoid most of that replicated code by using a naming scheme which allows you to associate your sets of 3 controls: eg see below. Then you can have one single procedure which performs the checks, given the first combobox as an argument.
e.g. (you can probably come up with more-meaningful names based on what your controls actually represent):
Then your code can look like this: