Option Explicit
'------------Protect Start---Excel880.com---QQ80871835-------
Public Sub E8_ProtectSheet(sht As Worksheet, ByVal flag As Boolean, Optional ByVal password = "")
'**根据工作表名保护或者解保护
'flag=true加保护 false解保护
'On Error Resume Next
If flag Then
sht.Protect password
Else
sht.Unprotect password
End If
End Sub
Public Sub E8_ProtectSheets(shtlist, Optional flag As Boolean = True, Optional password = "")
'**批量工作表保护或者解保护
'shtslist待操作工作表名列表 以逗号分隔 例如
Dim shts As Sheets, sht As Worksheet, wbk
Set wbk = ThisWorkbook
Set shts = wbk.Sheets(Split(shtlist, ","))
For Each sht In shts
Call E8_ProtectSheet(sht, flag)
Next
End Sub
Public Sub E8_ProtectAllSheets(Optional flag As Boolean = True, Optional password = "")
'**批量工作表保护或者解保护
'shtslist待操作工作表名列表 以逗号分隔 例如
Dim shts As Sheets, sht As Worksheet, wbk
Set wbk = ThisWorkbook
Set shts = wbk.Sheets
For Each sht In shts
Call E8_ProtectSheet(sht, flag, password)
Next
End Sub
'------------Protect End---------------
Private Sub Test()
Dim i&, j&, k&, arr, brr
'按列表保护
E8_ProtectSheets "sheet1,sheet2,sheet3,x", True, 123 '加保护
'E8_ProtectSheets "sheet1,sheet2,sheet3,x", False, 123'解保护
'E8_ProtectAllSheets True, 123 '保护所有工作表
'E8_ProtectAllSheets False, 123 '解保护所有工作表
End Sub
Public Sub 批量设置() '本示例 根据参数表对多个工作表设置不同的保护状态和不同的保护密码
Dim p, i
For i = 2 To 5
E8_ProtectSheet ThisWorkbook.Sheets(Cells(i, 1).Value), Cells(i, 2), Cells(i, 3)
Next
End Sub