如何用密码保护按钮?

时间:2015-09-16 20:43:12

标签: vba button passwords

我想知道如何在运行宏之前用密码保护按钮?

我尝试了以下代码,效果很好,但是如果第三次密码不正确,我就无法退出。

strPassword = "HR"
For lTries = 1 To 3
    strPassTry = InputBox("Enter Password please", "RUN MACRO")
    If strPassTry = vbNullString Then
        Exit Sub
    End If
    bSuccess = strPassword = strPassTry
    If bSuccess = True Then Exit For
    MsgBox "Password incorrect"
Next lTries

1 个答案:

答案 0 :(得分:0)

@SLaks评论旁边......

strPassword = "HR"

For lTries = 1 To 3
    strPassTry = InputBox("Enter Password please", "RUN MACRO")
    If strPassTry = vbNullString Then
        Exit Sub
    End If
    bSuccess = strPassword = strPassTry
    If bSuccess Then Exit For
    MsgBox "Password incorrect"
Next lTries

If Not bSuccess Then 
    Msgbox "Wrong password supplied!"
    Exit Sub
End If