Private Sub Workbook_Open()
Dim Password As String
Dim CorrectPassword As String
' 设置正确的密码
CorrectPassword = "a123456"
' 弹出输入框要求用户输入密码
Password = InputBox("请输入密码以打开此工作簿:", "密码保护")
' 检查密码是否正确
If Password <> CorrectPassword Then
' 如果密码错误,显示消息框并关闭Excel
MsgBox "密码错误!工作簿将关闭。", vbCritical
Application.Quit
End If
End Sub
代码放到thisworkbook中