我有一个使用下面的子ProtectAll_ADMIN()代码的按钮。
当管理员已经保护工作簿时,将发生错误,而不检查WorkBook是否已受到保护。所以我想要一个消息框告诉管理员工作簿已经受到保护。下面的代码将告诉我它已经受到保护的消息,即使它还没有。如果我删除以下代码行,它将运行正常,但我又回到正方形,它将会出错,因为已经从我的调用mcr_HideRowsColumns_ADMIN()隐藏了一列。即我希望这个子例程分开,因为有很多工作表需要隐藏的列和行。
If ActiveWorkbook.ProtectStructure Then End
MsgBox ActiveWorkbook.Name & " is already protected.", _
vbCritical
Exit Sub
以下是完整的代码,我真的很感激有人的敏锐眼光:
Sub ProtectAll_ADMIN()
Dim S As Object
If ActiveWorkbook.ProtectStructure Then End
MsgBox ActiveWorkbook.Name & " is already protected.", _
vbCritical
Exit Sub
' To Hide all rows and columns for editing
Call mcr_HideRowsColumns_ADMIN
Dim pWord1 As String, pWord2 As String
pWord1 = InputBox("Please Enter the password")
If pWord1 = "" Then Exit Sub
pWord2 = InputBox("Please re-enter the password")
If pWord2 = "" Then Exit Sub
'Make certain passwords are identical
If InStr(1, pWord2, pWord1, 0) = 0 Or _
InStr(1, pWord1, pWord2, 0) = 0 Then
MsgBox "You entered different passwords. No action taken!"
Exit Sub
End If
For Each ws In Worksheets
ws.Protect Password:=pWord1
Next
MsgBox "All Sheets are Protected."
Exit Sub
'-------------------------------------------
Sheets("Home").Select
Range("A1").Select
End Sub
有什么想法?谢谢!
答案 0 :(得分:1)
我稍微重新设计了你的代码:
Sub ProtectAll_ADMIN()
Dim ws As Worksheet
Dim pWord1 As String
Dim pWord2 As String
For Each ws In Worksheets
If ws.ProtectContents Then
MsgBox ActiveWorkbook.Name & " is already protected.", vbCritical
Exit Sub
End If
Next ws
' To Hide all rows and columns for editing
Call mcr_HideRowsColumns_ADMIN
pWord1 = InputBox("Please Enter the password")
If pWord1 = "" Then Exit Sub
pWord2 = InputBox("Please re-enter the password")
If pWord2 = "" Then Exit Sub
'Make certain passwords are identical
If InStr(1, pWord2, pWord1, 0) = 0 Or InStr(1, pWord1, pWord2, 0) = 0 Then
MsgBox "You entered different passwords. No action taken!"
Exit Sub
End If
For Each ws In Worksheets
ws.Protect Password:=pWord1
Next
MsgBox "All Sheets are Protected."
End Sub