如何在 VBA 中禁用“插入”命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24627133/
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
How to disable "Insert" Command in VBA
提问by Eva FP
I need a macro to disable inserting cells, rows and columns. I have tried these unsuccessful tricks (among others):
我需要一个宏来禁用插入单元格、行和列。我尝试过这些不成功的技巧(除其他外):
Application.CommandBars("Cell").Enabled = False
ActiveSheet.Protect AllowInsertingRows:=False, AllowInsertingColumns:=False
Application.CommandBars("Worksheet Menu Bar").FindControl _
(ID:=296, Recursive:=True).Enabled = False
CommandBars("Cell").FindControl(, 21437).Enabled = False
In each trial I have retrieved errors or just achieved disabling the row's inserting when selecting a column and vice versa. How can I get my goal?
在每次试验中,我都检索到错误或只是在选择列时禁用了行的插入,反之亦然。我怎样才能达到我的目标?
回答by Our Man in Bananas
you should be able to accomplish this by protecting your sheet with the right parameters,
您应该能够通过使用正确的参数保护您的工作表来实现这一点,
try putting the below code in the object module for your worksheet (on the sheet tab in Excel, right-click and select View code):
尝试将以下代码放入工作表的对象模块中(在 Excel 的工作表选项卡上,右键单击并选择View code):
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'ProtectSheet
ActiveSheet.Protect AllowInsertingColumns:=False, AllowInsertingRows:=False, UserInterfaceOnly:=True
End Sub
UPDATE: you can also do or undo this in Excel with menu TOOLS>>PROTECTION>>PROTECT SHEETor on the ribbon the button is in the Reviewtab.
更新:您也可以使用菜单工具>>保护>>保护工作表在 Excel 中执行或撤消此操作,或者在功能区上的“审阅”选项卡中执行此操作。
EDIT: for how to disable specific menu commands and controls please see Excel MVP Ron de Bruin - Disable menus & controls
编辑:有关如何禁用特定菜单命令和控件的信息,请参阅Excel MVP Ron de Bruin - Disable menus & controls