这个ws.protect Password = "edc1" 或ws.protect Password := "edc1" 没有工作!
需要通过变量发送密码,修复这个bug!
试试这个,它对我有用:
一张纸
Sub Protect()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SHEET")
Dim strPassword As String: strPassword = "YourPassword"
ws.Protect Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
UserInterfaceOnly:=True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, _
AllowSorting:=False, AllowFiltering:=False, AllowUsingPivotTables:=False
End Sub
和
Sub UnProtect()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SHEET")
Dim strPassword As String: strPassword = "YourPassword"
ws.Unprotect Password:=strPassword
End Sub
所有工作表
Sub ProtectAllSheets()
Dim ws As Worksheet
Dim strPassword As String: strPassword = "YourPassword"
For Each ws In Worksheets
ws.Protect Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
UserInterfaceOnly:=True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, _
AllowSorting:=False, AllowFiltering:=False, AllowUsingPivotTables:=False
Next ws
End Sub
和
Sub UnProtectAllSheets()
Dim ws As Worksheet
Dim strPassword As String: strPassword = "YourPassword"
For Each ws In Worksheets
ws.Unprotect Password:=strPassword
Next ws
End Sub