vba 如何防止用户删除工作表,但保持所有其他打开状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13702186/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:41:46  来源:igfitidea点击:

How to prevent user from deleting sheet, but leave all else open

excel-vbaexcel-2007vbaexcel

提问by rohrl77

I have developed an Excel Application in which useres can enter custom categories. I let the user call up and hide this data entry worksheet with a button in the custom ribbon.

我开发了一个 Excel 应用程序,用户可以在其中输入自定义类别。我让用户使用自定义功能区中的按钮调用并隐藏此数据输入工作表。

Now I've realized that the user can accidentally delete these worksheets. How do I disable the delete command for this worksheet while leaving all else open?

现在我意识到用户可能会意外删除这些工作表。如何禁用此工作表的删除命令同时保持所有其他打开状态?

I've been searching the web but have come up empty on this.

我一直在网上搜索,但对此却一无所知。

This is for Excel 2007

这是用于 Excel 2007

Thanks

谢谢

回答by bonCodigo

Protect them.

保护他们。

Tools > Protection > Protect Worksheet.

Tools > Protection > Protect Worksheet.

Add the password and choose what actions your users should do in the sheets.

添加密码并选择您的用户应在工作表中执行的操作。

You can do the same using VBAtoo. Check the following link

你也可以使用VBA同样的方法。检查以下链接

Updated with a code for sheet level protect

更新了工作表级保护的代码

You may put the following code in the sheet that you need to manage any mischief ;)

您可以将以下代码放在您需要管理任何恶作剧的工作表中;)

Private Sub Worksheet_Activate()
    ThisWorkbook.Protect Password:="Password", Structure:=True
End Sub

Private Sub Worksheet_Deactivate()
    ThisWorkbook.Unprotect Password:="Password"
End Sub

But you see, when you have a book with 100 sheets and if you want 50 sheets to be protected. Then you gotta either save all the sheet indices into a very hidden sheet. Usee that list in a module level VBA code to trigger the protect. Because not everytime you will have sheets in asceding order. If sheet indices in an order you can simply iterate them.

但是你看,当你有一本书有 100 页时,如果你想要保护 50 页。然后您必须将所有工作表索引保存到very hidden sheet. 在模块级 VBA 代码中使用该列表来触发保护。因为并非每次您都会按升序排列工作表。如果按顺序排列索引,您可以简单地迭代它们。

Let me know if you like to have workbook level code as well.

如果您也喜欢工作簿级别的代码,请告诉我。