公共密码无法在受保护的工作表中识别

问题描述 投票:0回答:1

我在Module1中有子“ProtectAllSheets”,在Sheet1对象上有另一个子,如果有#REF则用于删除整行! Sheet1的C列出错。当工作表受到保护时它工作正常...但是当我关闭工作簿并再次打开它(它仍然受到保护)时,它不会删除带有错误的行,尽管它是“UserInterface = True”。如果我取消保护表单,它可以正常工作。似乎只要我关闭工作簿,一些信息就会“以某种方式”丢失......我只是无法理解代码到底有什么问题。

Option Explicit
Public pwd1 As String, pwd2 As String

Sub ProtectAllSheets()
Dim ws As Worksheet

  pwd1 = InputBox("Enter your password", "")
    If pwd1 = "" Then Exit Sub
  pwd2 = InputBox("Enter the password again", "")
    If pwd2 = "" Then Exit Sub
    'Checks if both the passwords are identical
    If InStr(1, pwd2, pwd1, 0) = 0 Or _
    InStr(1, pwd1, pwd2, 0) = 0 Then
    MsgBox "Please type the same password. ", vbInformation, ""
      Exit Sub
    End If
For Each ws In ActiveWorkbook.Sheets
    If ws.ProtectContents = False = True Then
      ws.Protect Password:=pwd1, UserInterFaceOnly:=True
    End If
        Next ws
MsgBox "Sheets are protected."
End Sub

Option Explicit

Sub Worksheet_Activate()
Dim sh As Worksheet
Dim c As Long
Set sh = ActiveSheet

    For c = 400 To 2 Step -1
        If IsError(Cells(c, 3)) Then
         Rows(c).EntireRow.Delete
        End If
    Next c

End Sub
excel vba passwords
1个回答
0
投票

Ok Folks,对于那些可能有类似问题的人。我知道这不是最好的解决方案,但它有效。看起来UserInterFaceOnly功能没有保存在文件中,只要关闭并重新打开工作簿,它就会消失。所以,这就是我所做的。步骤1.删除Sub ProtectAllSheets()步骤2.插入Workbook_Open过程。

现在......如果他们按下Alt + F11,有人仍然可以看到密码“1214”。我对此无能为力。

Sub Workbook_Open()

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
      If Not ws.ProtectContents Then
      ws.Protect Password:=1214, UserInterFaceOnly:=True
      End If
        Next ws
        
    MsgBox "Sheets are protected."

End Sub

步骤3.通过Worksheet_Activate()保持相同的Sub

Sub Worksheet_Activate()

Dim sh As Worksheet
Dim c As Long
Set sh = ActiveSheet

    For c = 400 To 2 Step -1
        If IsError(Cells(c, 3)) Then
            Rows(c).EntireRow.Delete
        End If
            Next c
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.