Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50121

Trying to use VB to protect and Unprotect a worksheet

$
0
0
Hi All,
How do I protect a specific worksheet (not necessarily the "active sheet") with VB. That particular sheet, I initially protected it using "password" the password. Then I'm trying to unprotect the sheet when the userform is initialized. Then when it's terminated, I want to re-protect it. Here is the code I'm trying to use...

To Unprotect
Code:

Private Sub UserForm_Initialize()
Dim PW As String
PW = "password"
Worksheets("Summary").Unprotect Password: PW
End Sub

To Protect (haven't really tested this one, just assuming it's wrong because the other doesn't work.
Code:

Private Sub UserForm_Terminate()
Dim PW As String
PW = "password"
Worksheets("Summary").protect Password: PW
End Sub

Can someone help me figure out the correct code? Thanks!

Viewing all articles
Browse latest Browse all 50121