简体   繁体   English

在VBA中为受保护的Excel工作表指定用户权限

[英]Specifying User Permissions in VBA for Protected Excel Sheet

I am working with a spreadsheet that is required to be protected before distribution for the sheer sake of data integrity. 我正在处理一个电子表格,为了数据完整性,我们需要在分发之前对其进行保护。 I have written a function that auto-populates a column based on a drop down list selection. 我编写了一个基于下拉列表选择自动填充列的函数。 I do not want the user to edit this column so I have it protected, however in order to auto-populate my process un-protects and re-protects the spreadsheet. 我不希望用户编辑此列,因此我对其进行了保护,但为了自动填充我的流程,请取消保护并重新保护电子表格。 This is where the issue lies. 这就是问题所在。

I would like users to have all other permissions (eg formatting, row insertion, row deletion, etc). 我希望用户拥有所有其他权限(例如格式化,行插入,行删除等)。 However, when the process re-protects the sheet, all permissions are revoked. 但是,当进程重新保护工作表时,将撤消所有权限。

Is there a way I can both lock the sheet AND specify which user permissions I would like to grant in VBA? 有没有办法可以锁定工作表并指定我想在VBA中授予哪些用户权限?

The Worksheet.Protect method allows to specify everything that would be available to you when performing the Review ► Changes ► Protect Worksheet command. Worksheet.Protect方法允许指定执行查看►更改►保护工作表命令时可用的所有内容。 The parameters are largely optional so they need to be specified explicitly or blank parameters can be passed in with commas as placeholders. 这些参数在很大程度上是可选的,因此需要明确指定它们,或者使用逗号作为占位符传递空白参数。

To protect a worksheet with a password and allow column formatting and row insertion: 使用密码保护工作表并允许列格式化和行插入:

With Worksheets("Sheet One")
    .Protect Password:="myPassword", Contents:=True, _
             AllowFormattingColumns:=True, AllowInsertingRows:=True
    'insert a row
    .Rows("9:9").EntireRow.Insert CopyOrigin:=xlFormatFromLeftOrAbove
End With

See the Worksheet.Protect method for a full list of available options. 有关可用选项的完整列表,请参阅Worksheet.Protect方法

Another option is the UserInterfaceOnly . 另一个选项是UserInterfaceOnly This stops the user from predetermined actions on the worksheet but allows VBA procedures to perform actions that would otherwise be restricted. 这会阻止用户对工作表上的预定操作,但允许VBA过程执行否则将受到限制的操作。

With Worksheets("Sheet One")
    .Protect Password:="myPassword", UserInterfaceOnly:=True, Contents:=True, _
                 AllowFormattingColumns:=True, AllowInsertingRows:=True
    'insert a column; the user cannot do this
    .Columns(2).EntireColumn.Insert CopyOrigin:=xlFormatFromLeftOrAbove
End With

This latter behavior allow you more freedom in your VBA procedures without having to continually unprotect and reprotect the worksheet. 后一种行为允许您在VBA过程中获得更多自由,而无需不断地取消保护和重新保护工作表。

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

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