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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16125272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:45:14  来源:igfitidea点击:

Run-time error '1004': Unable to set the enabled property of the OLEObject class

excelexcel-vbavba

提问by user388197

I got the above error message when I set combobox2.enable = Truewhen there's a change in combobox1.

当我combobox2.enable = True在组合框 1 中发生更改时进行设置时,我收到了上述错误消息。

Private Sub ComboBox1_Change()

   If ComboBox1.Value = "One Session" Then
      ComboBox2.Enabled = True
   End If
End Sub

Can someone please tell me what's went wrong? Thanks

有人可以告诉我出了什么问题吗?谢谢

回答by ExcelVbaIsFun

Make sure the names of each combobox are ComboBox1 and ComboBox2. If you renamed them, it could give errors such as this.

确保每个组合框的名称是 ComboBox1 和 ComboBox2。如果您重命名它们,则可能会出现诸如此类的错误。

Plus if you've got combobox2 disabled (Enabled=false) for some reason, this is what you need, however if you're trying to have excel set the focus onto ComboBox2, use ComboBox2.SetFocus

另外,如果您出于某种原因禁用了 combobox2(启用 = 假),这就是您所需要的,但是如果您尝试让 excel 将焦点设置在 ComboBox2 上,请使用 ComboBox2.SetFocus

回答by Siddharth Rout

I believe that the combobox is in your worksheet.

我相信组合框在您的工作表中。

There are two main reasons for getting this error.

出现此错误的主要原因有两个。

  1. ActiveX Controls are disabled. You may want to see THISOR

  2. Your worksheet is protected. If your worksheet is protected then you have two ways to handle it

  1. ActiveX 控件被禁用。你可能想看看这个

  2. 您的工作表受到保护。如果您的工作表受到保护,那么您有两种方法来处理它

Way 1

方式一

Private Sub ComboBox1_Change()
    ActiveSheet.Unprotect "YOUR PASSWORD"
    If ComboBox1.Value = "One Session" Then
       ComboBox2.Enabled = True
    End If
    ActiveSheet.Protect "YOUR PASSWORD"
End Sub

OR

或者

Way 2

方式二

This uses UserInterfaceOnly:=Truewhich allows you to make changes to the sheet only using macros

这使用UserInterfaceOnly:=True它允许您仅使用宏更改工作表

ActiveSheet.Protect Password:="YOUR PASSWORD", _
DrawingObjects:=True, Contents:=True, _
Scenarios:=True, UserInterfaceOnly:=True

回答by Codes with Hammer

I keep running into this issue, and my resolution is slightly different from the prior two answers. In my case, the objectis locked, as well as the worksheet. My resolution is to unprotect the worksheet, select the object causing the error, go into its properties, and uncheck "Locked". Then reprotect the worksheet and save.

我一直遇到这个问题,我的解决方案与前两个答案略有不同。就我而言,对象和工作表都已锁定。我的解决方案是取消保护工作表,选择导致错误的对象,进入其属性,然后取消选中“锁定”。然后重新保护工作表并保存。