使用 VBA 重置当前文件的密码时,重新保护后“格式化列和格式化行、自动过滤器”选项将被禁用。我们不需要所有文件都使用这些选项,因为根据要求将启用这些选项,并且如果我们与用户共享密码,将通过宏重置密码,此时这些选项将被禁用。任何人都可以针对我的查询提供解决方案。谢谢你。
Sub Main_Protect()
Dim PWD As String
Dim WB As Workbook
Dim ws As Worksheet
Dim rng As Range, cell As Range
Dim sht As Worksheet
PWD = ThisWorkbook.Sheets("Main").Range("D1").Value
'If PWD = "" Then
'PWD = InputBox("Please Enter the password")
'End If
Set WB = Application.Workbooks.Open(ThisWorkbook.Sheets("Main").Cells(13, 1))
' Loop through all sheets in the workbook
LR = ThisWorkbook.Sheets("Tab").Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Set rng = ThisWorkbook.Sheets("Tab").Range("A2:A" & LR)
For Each cell In rng
If cell > 0 Then
WB.Activate
Set ws = WB.Sheets(cell.Value)
ws.Protect PWD,Contents:=True, UserInterfaceOnly:=True
ThisWorkbook.Sheets("Tab").Range("B" & ThisWorkbook.Sheets("Tab").Cells(Rows.Count, 2).End(xlUp).Row + 1).Value = "Yes"
End If
Next cell
MsgBox "Done"
WB.Save
WB.Close
End Sub
是否有任何选项在以前相同的选项上启用的选项在保护后应该可用。
为了在保护工作表时启用预期的功能,请指定相应的相关参数。
ws.Protect PWD, Contents:=True, UserInterfaceOnly:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True _
, AllowFiltering:=True
微软参考文档: