我试图在工作表打开时禁用一堆控件(然后在工作表关闭时重新启用它们),但是当打开工作表时(代码运行时),我得到错误“对象变量或与块变量未设置。这是我的代码:
选项明确
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
答案 0 :(得分:1)
首先,在工作簿打开时,不会填充命令栏。将代码粘贴在宏中,并在填充完所有内容后运行它。第二,没有控制ID 3181(只是fyi)我循环遍历所有的id并且没有3181,这是列表中的第二个。所以,你需要在那里添加一些错误处理。像这样
If Not myControle Is Nothing Then
'do the loop code
End If
因为未找到特定控件时,myControl
设置为Nothing
因为命令栏没有在工作簿上打开,将代码放在工作簿的“SheetActivate”方法中,然后在工作簿中打开put
Sheets(2).Activate
Sheets(1).Activate
这会强制触发sheetactivate事件。它有两次,因为如果工作簿在您放置的工作表上打开(在我的示例中为sheet2),则激活不会触发。所以hense,激活工作表2,然后工作表1(工作簿打开时,工作表2已经打开)