【问题标题】:VBA unprotect-blank entry and exit handling [duplicate]VBA unprotect-blank进入和退出处理[重复]
【发布时间】:2021-09-27 01:00:25
【问题描述】:

下面的代码取消保护工作簿中的所有工作表,并且只提示输入一次密码。

我想要实现的是:

如果用户在密码输入窗口按“取消”,子程序退出。 如果用户在没有输入任何内容的情况下按“ok”,它的行为应该与输入错误密码相同,即转到错误弹出窗口。

问题在于按“确定”或“取消”时,它的行为与上述不同,相反,在这两种情况下,它都会再显示 3 次默认密码提示,每张纸 1 次。

我正在努力完善 if/then 逻辑,并且已经多次交换了一些东西,几乎到了那里,但从未完全做到。

Sub UnprotectAllSheets()
    Dim ws As Worksheet
    Dim pass As String
    
    If ActiveSheet.ProtectContents = False Then
        MsgBox "Already Unprotected" 
        Exit Sub
    Else

        pass = InputBox("Password?")
        On Error GoTo Popup:
        
        For Each ws In ThisWorkbook.Worksheets
            ws.Unprotect pass
        Next ws
        
        If ActiveSheet.ProtectContents = False Then
            MsgBox "Sheets now Unprotected"
            
        ElseIf StrPtr(pass) = "" Then  'if press OK on blank entry
            MsgBox "Incorrect Password", vbCritical, "Admin"
            
        ElseIf pass = 0 Then 'if press CANCEL
            Exit Sub
        End If
    End If
Popup:
    If err.Number = 1004 Then
        MsgBox "Incorrect Password", vbCritical, "Admin"
    End If
End Sub

【问题讨论】:

    标签: excel vba if-statement


    【解决方案1】:

    InputBox 在按Cancel、输入空字符串或按窗口角 x 的情况下的行为类似,就String 返回而言。

    下一个 sub 提供了将上述情况分开的可能性:

    Sub testInputBox()
       Dim pass
        pass = InputBox("Password?")
        'standard behavior without checking the result:
        MsgBox "Pass is " & pass 'it will be an empty string in case of Cancel, empty string, pressing window corner X
        If StrPtr(pass) = 0 Then
            MsgBox ("Cancel pressed...") 'the same message if window corner `X` is pressed
        ElseIf pass = vbNullString Then
            MsgBox ("Empty string..")    'OK for an empty string
        Else
            MsgBox ("You entered " & pass)
        End If
    End Sub
    

    如果使用上述识别方式,则无需错误处理。

    我应该修改你的代码以这样的方式行动:

    Sub UnprotectAllSheets()
        Dim ws As Worksheet, pass As String, myPass As String, i As Long
        
            myPass = "1234"
    TryAgain:
            pass = InputBox("Password?")
            If StrPtr(pass) = 0 Then Exit Sub 'for Cancel and window corner 'X' pressed
            If pass = vbNullString Then MsgBox "You did  not enter any password!", vbCritical, "Admin": Exit Sub
            If pass <> myPass Then
                
                MsgBox "Incorrect Password" & vbCrLf & _
                           IIf(i < 3, "Please, try again!", "We stop here..."), vbCritical, "Admin"
                 If i >= 3 Then Exit Sub
                i = i + 1
               
                GoTo TryAgain
            End If
            For Each ws In ThisWorkbook.Worksheets
               If ws.ProtectContents = True Then
                    ws.Unprotect pass
               End If
            Next ws
    End Sub
    

    【讨论】:

    • 谢谢你这很好用
    • 我现在正在开车......我会在我在家时修改代码。但是我是不是从三个尝试的角度错误地理解了你的问题?
    • 我现在已经设法修改代码以删除 3 次尝试。我试图在您阅读之前立即删除该评论。我实际上想知道为什么您将 3 次尝试包括在内,现在我发现存在误解。我的代码遇到的问题是,在 vbNullString 或 Cancel 之后,它会显示默认密码窗口 3 次,这不是我想要的。谢谢你的帮助:)
    猜你喜欢
    • 2021-08-26
    • 2023-04-09
    • 2017-08-08
    • 2013-01-30
    • 2016-05-02
    • 2019-08-08
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    相关资源
    最近更新 更多