我正在尝试编写一个代码,要求输入密码才能打开Excel文件。应该有三个密码(例如美国,亚洲,欧洲)。根据输入的密码,只能启用某些范围进行编辑(例如A2的“美国”:A100,B2的“亚洲”:B100,C2的“欧洲”:C100)。
答案 0 :(得分:1)
按如下方式设置工作簿:
将以下代码添加到工作簿
Private Sub Workbook_Open()
Dim password As String
password = InputBox("Password", "Please enter the password")
Dim worksheetpassword As String
worksheetpassword = "password"
If password = "AmericaPassword" Then
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = False
ActiveSheet.Range("Asia").Locked = True
ActiveSheet.Range("Europe").Locked = True
ActiveSheet.Protect (worksheetpassword)
ElseIf password = "AsiaPassword" Then
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = True
ActiveSheet.Range("Asia").Locked = False
ActiveSheet.Range("Europe").Locked = True
ActiveSheet.Protect (worksheetpassword)
ElseIf password = "EuropePassword" Then
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = True
ActiveSheet.Range("Asia").Locked = True
ActiveSheet.Range("Europe").Locked = False
ActiveSheet.Protect (worksheetpassword)
Else
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = True
ActiveSheet.Range("Asia").Locked = True
ActiveSheet.Range("Europe").Locked = True
ActiveSheet.Protect (worksheetpassword)
MsgBox ("You cannot edit this file")
End If
End Sub
密码保护您的工作簿 - 代码假定密码为“密码”
关闭工作簿并重新打开,系统将提示您输入密码。如果您输入“AmericaPassword”,您应该可以修改“America”范围,“AsiaPassword”来修改“AsiaPassword”等。