如何修复 运行-自定义命令栏 VBA 中的时间错误“91”

How to fix Run-Time error '91' in custom command bar VBA

我有一个包含多个 sheet 的 excel 工作簿,我试图将用户的能力限制为第一个工作 sheet 中的 insert/delete 行。我目前有 VBA 代码删除(隐藏)行菜单中的 'Insert' 和 'Delete' 按钮,并插入新的假 insert/delete 按钮,这两个按钮都会创建弹出-向上框指示用户如何正确 remove/add 行(我有一些键绑定宏,它们同时 delete/add 行跨多个 select sheets)。

此代码有时有效,但大多数情况下 returns 出现“运行 时错误 '91':未设置对象变量或 With 块变量”。

从大量类似的问题中挑选出来,我确定我很可能没有正确初始化命令栏对象(或类似的东西),但我一直无法弄清楚如何正确地做到这一点。谁能帮忙改正错误?

这是 sheet 对象中的代码:

Private Sub Worksheet_Activate()

'reset to standard context menu before adding new option
Application.CommandBars("Row").Reset

'removes standard Delete and Insert menu bar items
Application.CommandBars("Row").FindControl(ID:=293).Visible = False
Application.CommandBars("Row").FindControl(ID:=296).Visible = False
        
'add custom row deletion call
With Application.CommandBars("Row").Controls.Add
    .Caption = "Delete Row"
    .Style = msoButtonCaption
    .OnAction = "DeleteRow"
End With
With Application.CommandBars("Row").Controls.Add
    .Caption = "Insert Row"
    .Style = msoButtonCaption
    .OnAction = "InsertRow"
End With

End Sub

Private Sub Worksheet_Deactivate()
'get rid of the customization when you're done with this sheet
Application.CommandBars("Row").Reset

End Sub

这是工作簿对象中的代码:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
'resets menu bars when workbook sheets are changed
Application.CommandBars("Row").Reset
End Sub

Private Sub Workbook_Deactivate()
    'Resets menu bars when workbook is deactivated
    Application.CommandBars("Row").Reset
End Sub

这是添加按钮的模块 -

模块 1:

Public Sub DeleteRow()

    MsgBox "Manually deleting rows from this Estimate will disconnect linked sheets. Please use CTRL-Q to delete rows.", vbOKOnly, "WARNING: Delete Rows"

End Sub

模块 2:

Public Sub InsertRow()

    MsgBox "Manually inserting rows into this Estimate will disconnect linked sheets. Please use CTRL-A to add rows.", vbOKOnly, "WARNING: Insert Row"

End Sub

将这些控件的可见性设置为 false 似乎是在修改集合,删除您要查找的 ID。

查看 ID 的代码:

Sub test()
    Dim ctl As Object
    
    For Each ctl In Application.CommandBars("Row").Controls
        Debug.Print ctl.ID, ctl.Caption, ctl.TooltipText
    Next
    Debug.Print vbNewLine, vbNewLine
End Sub

如果我运行修改之前是这样的:

ID Caption Tooltip
21 Cu&t Cu&t
19 &Copy &Copy
22 &Paste &Paste
21437 Paste &Special... Paste &Special...
3624 &Paste Table &Paste Table
32713 Data T&ype
296 &Rows Insert Rows
293 &Delete &Delete
3125 Clear Co&ntents Clear Co&ntents
855 &Format Cells... &Format Cells...
541 &Row Height... &Row Height...
883 &Hide &Hide (Ctrl+9)
884 &Unhide &Unhide (Ctrl+Shift+( )
3626 &Remove Hyperlinks &Remove Hyperlinks

运行 如你所见 ID:=293 首先:

ID Caption Tooltip
21 Cu&t Cu&t
19 &Copy &Copy
22 &Paste &Paste
21437 Paste &Special... Paste &Special...
3624 &Paste Table &Paste Table
32713 Data T&ype
3183 &Insert Insert Cells
293 &Delete &Delete
3125 Clear Co&ntents Clear Co&ntents
855 &Format Cells... &Format Cells...
541 &Row Height... &Row Height...
883 &Hide &Hide (Ctrl+9)
884 &Unhide &Unhide (Ctrl+Shift+( )
3626 &Remove Hyperlinks &Remove Hyperlinks

您可以看到 ID 296 从列表中消失了,现在出现了 3183。手动测试它做同样的事情,添加一行。

交换行并成功 运行ning 您的脚本后:

ID Caption Tooltip
21 Cu&t Cu&t
19 &Copy &Copy
22 &Paste &Paste
21437 Paste &Special... Paste &Special...
3624 &Paste Table &Paste Table
32713 Data T&ype
3183 &Insert Insert Cells
293 &Delete &Delete
3125 Clear Co&ntents Clear Co&ntents
855 &Format Cells... &Format Cells...
541 &Row Height... &Row Height...
883 &Hide &Hide (Ctrl+9)
884 &Unhide &Unhide (Ctrl+Shift+( )
3626 &Remove Hyperlinks &Remove Hyperlinks
1 Delete Row Delete Row
1 Insert Row Insert Row

无论什么 296 似乎都被替换了,而 293 仍然存在。我现在无法重现它,但在测试时我看到 ID 3181 而不是 3183,所以我不会依赖该值。

我不知道为什么会这样做。