【问题标题】:Trying to Disable Command Bar Controls on Worksheet Open VBA尝试在工作表打开 VBA 上禁用命令栏控件
【发布时间】: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

【问题讨论】:

    标签: vba excel worksheet


    【解决方案1】:

    首先,在打开工作簿时,不会填充命令栏。将您的代码粘贴在宏中并在填充完所有内容后运行它。其次,没有 id 3181 的控制(仅供参考)我遍历了所有的 id 并且没有 3181,这是列表中的第二个。因此,您需要在其中添加一些错误处理。像这样

    If Not myControle Is Nothing Then
    
    'do the loop code
    
    End If
    

    因为当找不到特定控件时,myControl 会被设置为 Nothing

    编辑

    因为打开工作簿时没有填充命令栏,所以将代码放在工作簿的“SheetActivate”方法中,然后在工作簿打开放

    Sheets(2).Activate
    Sheets(1).Activate
    

    这将强制触发 sheetactivate 事件。它在那里两次,因为如果工作簿在您首先放置的工作表上打开(在我的示例中为 sheet2),则激活不会触发。因此,激活工作表 2,然后激活工作表 1(如果工作簿打开时工作表 2 已经打开)

    【讨论】:

    • 也许您有不同版本的 excel,但在 2010 年,它在单元格命令栏上是“插入...”:link。至于另一个问题,我需要此代码在工作表打开和关闭时自动运行,因此用户不能干预某些事情。用户运行的宏不适用于我的情况。
    • 我的可能没有插入栏。无论如何,错误检查是一件好事。我编辑了我的答案,以便代码能够触发和工作
    • 好的,当我打开工作表时,它可以禁用我的选项,但是当我关闭它时它不能重新启用它们。 “Workbook_Close()”不存在吗?
    • 没关系,你必须使用它作为你的子:Workbook_BeforeClose(Cancel As Boolean)
    • 哦,我完全忘记了之后重新启用。是的。所以在工作簿中关闭只需调用启用子
    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 2021-05-15
    • 2019-04-30
    • 2020-03-06
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多