基于COMBOBOX的对象所需的错误启用/禁用Combobox/复选框

发布于 2025-01-28 17:42:51 字数 715 浏览 2 评论 0原文

我正在研究一个重复系列的表,一个连击链接链接到另一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2025-02-04 17:42:51

仅供参考,您可以使用命名方案避免大部分复制的代码,该方案允许您将3个控件的集合关联:例如,请参见下文。然后,您可以使用一个单个过程,将第一个组合作为参数,可以执行检查。

例如(您可能会根据您的控件的实际代表提出更多的名称):

ComboBox1 - ComboBox1_2 - chkComboBox1
ComboBox2 - ComboBox2_2 - chkComboBox2
ComboBox3 - ComboBox3_2 - chkComboBox3

那么您的代码看起来像这样:

Option Explicit

Private Sub ComboBox1_Change()
    ProcessChange ComboBox1
End Sub

Private Sub ComboBox2_Change()
    ProcessChange ComboBox2
End Sub

'process the supplied combobox value and set properties of
'  any related controls
Sub ProcessChange(cmbo As ComboBox)
    Dim en As Boolean
    en = cmbo.Value = 2 Or cmbo.Value = 3
    Me.OLEObjects(cmbo.Name & "_2").Object.Enabled = en
    Me.OLEObjects("chk" & cmbo.Name).Object.Enabled = en
End Sub

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):

ComboBox1 - ComboBox1_2 - chkComboBox1
ComboBox2 - ComboBox2_2 - chkComboBox2
ComboBox3 - ComboBox3_2 - chkComboBox3

Then your code can look like this:

Option Explicit

Private Sub ComboBox1_Change()
    ProcessChange ComboBox1
End Sub

Private Sub ComboBox2_Change()
    ProcessChange ComboBox2
End Sub

'process the supplied combobox value and set properties of
'  any related controls
Sub ProcessChange(cmbo As ComboBox)
    Dim en As Boolean
    en = cmbo.Value = 2 Or cmbo.Value = 3
    Me.OLEObjects(cmbo.Name & "_2").Object.Enabled = en
    Me.OLEObjects("chk" & cmbo.Name).Object.Enabled = en
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文