[英]Excel Run Macro button with password protection
我的工作簿受密碼保護,由用於輸入的未鎖定單元格和用於PDF打印的ane按鈕組成。 由於工作表的密碼保護,PDF打印按鈕不起作用。
我已經嘗試了以下早期問題和解答。 但是它說我輸入的密碼是錯誤的(!)
當工作表受保護時,宏不起作用。 正在運行的宏返回運行時錯誤1004
Sub ButtonClick()
UnprotectAll
'Some stuff here
ProtectAll
End Sub
Private Const yourPassword As String = "ThePassWord"
Sub UnprotectAll()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect Password:=yourPassword
Next sh
End Sub
Sub ProtectAll()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=yourPassword
Next sh
End Sub
我得到了
錯誤“ 1004”。
您提供的密碼不正確。
確保在每個模塊和工作表代碼中將Option Explicit
用作第一行! 這樣可以確保您不會遇到未聲明的局部范圍變量yourPassword
,該變量隨后為空。
我建議始終激活Option Explicit
:在VBA編輯器中,轉到“ 工具” ›“ 選項” ›“ 需要變量聲明” 。
確保使用密碼取消(手動)保護所有工作表。
在模塊中將所需的新密碼定義為常量
Private Const yourPassword As String = "ThePassWord"
使用此常數您.Protect
/ .Unprotect
sh.Protect Password:=yourPassword
然后再次運行您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.