我有一个宏来解锁我的excel文档,拼写检查,然后锁定工作表。但是,在锁定工作表时,它会删除有关用户可以在锁定的电子表格中执行哪些操作的规则(例如:添加/删除行、更改框高/框宽等)
我曾尝试在.Protect Password之后列出规则,但它不起作用。
Sub ProtectSheetCheckSpellCheck()
'Update by Extendoffice 2018/11/2
Dim xRg As Range
On Error Resume Next
Application.ScreenUpdating = False
With ActiveSheet
.Unprotect ("Password123")
Set xRg = .UsedRange
xRg.CheckSpelling
.Protect Password:="Password123", AllowInsertingRows:=True, AllowInsertingColumns:=True
End With
Application.ScreenUpdating = True
End Sub发布于 2019-02-08 06:57:46
您正在寻找的是能够在不实际取消保护的情况下修改您的工作表。您可以保护工作表,但允许VBA进行更改,而用户不能:
ActiveSheet.Protect UserInterfaceOnly:=True
https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.protect
姓名: UserInterfaceOnly
说明:为True,则保护用户界面,但不保护宏。如果省略此参数,则保护同时应用于宏和用户界面。
这假设在工作表最初受到保护时,该选项设置为True。
或者,您可以将当前保护设置的状态存储在Protection对象中:
Dim myProtection as Protection Set myProtection = ActiveSheet.Protection
如下所示:请注意,由于某些原因,.AllowEditRanges在读取时会返回一个错误,所以我没有将其包括在列表中。
Sub test()
'Pick your Worksheet
Dim mySheet As Worksheet
Set mySheet = ActiveSheet
'Unprotect it
Dim myProtection As Protection
Set myProtection = mySheet.Protection
mySheet.Unprotect
'Do your VBA things
'Re Protect it
With myProtection
mySheet.Protect AllowFormattingCells:=.AllowFormattingCells, _
AllowFormattingColumns:=.AllowFormattingColumns, _
AllowFormattingRows:=.AllowFormattingRows, _
AllowInsertingColumns:=.AllowInsertingColumns, _
AllowInsertingRows:=.AllowInsertingRows, _
AllowInsertingHyperlinks:=.AllowInsertingHyperlinks, _
AllowDeletingColumns:=.AllowDeletingColumns, _
AllowDeletingRows:=.AllowDeletingRows, _
AllowSorting:=.AllowSorting, _
AllowFiltering:=.AllowFiltering, _
AllowUsingPivotTables:=.AllowUsingPivotTables
End With
End Sub注意:.Protect包括DrawingObjects、Scenario和USerInterfaceOnly,而这些设置不是由Protection对象提供的。我知道Contents参数应该与.AllowEditRanges相关,但是由于它不能被访问,我猜它也会被跳过,
https://stackoverflow.com/questions/54583471
复制相似问题