简体   繁体   English

Excel VBA保护返回错误

[英]Excel VBA protect returning error

I've seen many posts on here and elsewhere about using the following code in the ThisWorkbook Object in order to protect your workbook but still allow macros to run: 我在这里和其他地方看到过很多关于在ThisWorkbook对象中使用以下代码以保护您的工作簿但仍允许运行宏的文章:

Private Sub Workbook_Open()
    ActiveWorkbook.Protect UserInterfaceOnly:=True
End Sub

However, the protect function tooltip that pops up only offers three arguments Protect([password], [structure], [windows]). 但是,弹出的保护功能工具提示仅提供三个参数Protect([password],[structure],[windows])。 Therefore, when I open the document I get the following error 因此,当我打开文档时,出现以下错误

"Compile Error: Named argument not found" “编译错误:找不到命名参数”

in regard to UserInterfaceOnly:=True . 关于UserInterfaceOnly:=True

Am I using the wrong protect function, or is there something else I'm missing? 我使用了错误的保护功能,还是我缺少其他功能? I want to allow users to input data into the workbook using forms/macros, but I don't want them to be able to manually change any cells. 我想允许用户使用表格/宏将数据输入到工作簿中,但是我不希望他们能够手动更改任何单元格。

It almost seems as if the protect function that I'm running has different arguments than many of the other protect solutions I've found. 我正在运行的保护功能似乎与我发现的许多其他保护解决方案具有不同的参数。 Could this be a version issue or something else? 这可能是版本问题还是其他问题?

The suggestion provided by Tim Williams worked. 蒂姆·威廉姆斯Tim Williams)提供的建议奏效了。 Protect doesn't work the same way at the workbook level, therefore I needed to write individual code for each sheet I wanted to protect. 保护在工作簿级别的工作方式不同,因此我需要为要保护的每个工作表编写单独的代码。 I used the following code to protect the sheets but allow forms to populate: 我使用以下代码保护工作表,但允许填写表格:

Private Sub Workbook_Open()
    Worksheets("Sheet1").Protect UserInterfaceOnly:=True
    Worksheets("Sheet2").Protect UserInterfaceOnly:=True
    Worksheets("Sheet3").Protect UserInterfaceOnly:=True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM