Excel:combobox和checkbox干扰?

我正在用一些ActiveX控件制作一个Excel表单,并且遇到了包含以下function的问题:

在这里输入图像说明

我希望用户在ComboBox11中select一个号码。 如果这个数字是0,那么表单会改变,那么combobox9和10将被禁用(使用VBA代码),下面的表格将会被淡出(使用条件格式),通知用户不要填写。

另一方面,如果用户select大于0的数字,则表单将保持原样。 在表格下是一个checkbox(checkbox1),如果需要放入表单中的数据大于表格大小,则用于展开表格(取消隐藏先前隐藏的行)。

combobox11后面的VBA代码是:

Private Sub ComboBox11_change() Dim ws As Worksheet Set ws = Sheets("Form") If Not Me.ComboBox11.Text = "" Then ws.Range("J24") = CInt(Me.ComboBox11.Text) 'to write integer instead of text into linked cell If Me.ComboBox11.Value = 0 Then Me.ComboBox9.Enabled = False Me.ComboBox10.Enabled = False If Me.CheckBox1.Value = True Then Me.CheckBox1.Value = False 'if combobox11 is 0 than the table doesn't need to be expanded Me.CheckBox1.Enabled = False Else Me.ComboBox9.Enabled = True Me.ComboBox10.Enabled = True Me.CheckBox1.Enabled = True End If End Sub 

CheckBox1背后的代码是:

  Private Sub CheckBox1_Change() Dim ws As Worksheet Set ws = Sheets("Form") If CheckBox1.Value = False Then ws.Rows("46:71").Hidden = True If CheckBox1.Value = True Then ws.Rows("46:71").Hidden = False End Sub 

所以,如果我在combobox11中select0,结果是:

在这里输入图像说明

到现在为止还挺好。 但是,如果我select大于0的东西,在combobox11中说1,然后尝试通过单击checkbox1来展开表格,表格展开,但是我收到一条错误消息:

运行时错误“1004”:无法设置属性:启用类:OLEObject

(不知道确切的错误文本,因为我不使用英文MSOffice)

按下“debugging”button时, Sub ComboBox11_Change()的以下行将亮起:

 Me.ComboBox9.Enabled = True 

奇怪的是,当combobox11保持空白(没有select一个值)时,这个错误不会出现。

我不知道为什么checkbox会与其他combobox交互。 我感到困惑,任何帮助将不胜感激。

要重现这一点:

有这样一个表: 在这里输入图像说明

A1:A6是ComboBox的ListFillRange。

然后在CheckBox1_Change()中使用VBA代码在1:6之间隐藏任何​​行将会抛出错误。

 Private Sub CheckBox1_Change() If CheckBox1.Value = False Then Me.Rows("7:13").Hidden = True If CheckBox1.Value = True Then Me.Rows("7:13").Hidden = False 'If CheckBox1.Value = False Then Me.Rows("6:13").Hidden = True 'ERROR 'If CheckBox1.Value = True Then Me.Rows("6:13").Hidden = False 'ERROR End Sub Private Sub ComboBox1_Change() If Me.ComboBox1.Value = 0 Then If Me.CheckBox1.Value = True Then Me.CheckBox1.Value = False Me.CheckBox1.Enabled = False Else Me.CheckBox1.Enabled = True End If End Sub 

如果我们创build一个名为“list”的名字,这个名字就像这样一个整列: 在这里输入图像说明 并使用这个“list”作为ComboBox的ListFillRange,那么如果这个表中的行被代码隐藏,就会发生错误。 这与隐藏行是否在真正的活动“列表”行1-6中无关。

如果我们不在名字中引用整个列,但仅举例来说只有1-10行:

 =INDEX(Sheet1!$A$1:$A$10;1):INDEX(Sheet1!$A$1:$A$10;6) 

那么错误只发生在代码隐藏1到10行的情况下,而不隐藏11行以上的行。

编辑:

继续我的例子:从Sheet1中移动数字!A:A到Sheet2!A:A并创build一个名为范围的“list”

 =Sheet2!$A$1:INDEX(Sheet2!$A$1:$A$40;COUNTIF(Sheet2!$A$1:$A$40;"<>")) 

然后下面的代码将Sheet1.ComboBox1.ListFillRange设置为“列表”将工作,如果它在Sheet2类模块内:

 Private Sub Worksheet_Change(ByVal Target As Range) ThisWorkbook.Worksheets(1).ComboBox1.ListFillRange = "list" End Sub 

如果在Sheet1类模块中设置ListFillRange,则会产生错误。

由于意见太短,无法解释我做了解决这个问题,我张贴这个答案。

首先感谢Axel Richter,他花时间阐述了我的问题。

Combobox11在名称pipe理器中填充了生成的ListFillRange:

在这里输入图像描述

所描述的错误停止出现,只要我改变了ListFillRange。 起初,我尝试了一个简单的select: "=list!AU1:AU40"虽然我不明白这个问题现在已经解决了!

之后,我使用下面的代码来为combobox11创build一个dynamic列表。

  Dim zadnji As Integer zadnji = Sheets("Form").Range("T9").Value + 1 Me.OLEObjects("combobox11").ListFillRange = "=lists!AU1:AU" & zadnji