【发布时间】:2016-04-15 12:49:30
【问题描述】:
我试图在工作表打开时禁用一堆控件(然后在工作表关闭时重新启用它们),但是在打开工作表时(当代码运行时),我收到错误“对象变量或 With块变量未设置。这是我的代码:
选项显式
Private Sub Workbook_Open()
OptionsDisable
End Sub
Private Sub Workbook_Close()
OptionsEnable
End Sub
Sub OptionsDisable()
Dim myControls As CommandBarControls
Dim ctl As CommandBarControl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=21)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=3181)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=292)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=3125)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=855)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=1576)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=293)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=541)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=3183)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=294)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=542)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=886)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=887)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=883)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=884)
For Each ctl In myControls
ctl.Enabled = False
Next ctl
End Sub
Sub OptionsEnable()
Dim myControls As CommandBarControls
Dim ctl As CommandBarControl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=21)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=3181)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=292)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=3125)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=855)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=1576)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=293)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=541)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=3183)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=294)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=542)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=886)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=887)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=883)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, ID:=884)
For Each ctl In myControls
ctl.Enabled = True
Next ctl
End Sub
【问题讨论】: