Protect / unprotect worksheet through vba

shanzek

New Member
Joined
Feb 23, 2004
Messages
19
I am creating workbookA by taking a templateA and pasting in information from workbookB. The goal is to have the finished workbookA with some cells locked, and some unlocked. My code works fine until I tried to protect the worksheet in the template. Now it can't paste info into it, since it is locked.

I figure my two choices are:
(1) start with template unprotected, paste info in, then protect it
(2) start with template protected, unprotect it in vba, paste in info, protect it

Any suggestions as to the best approach? I'm not protecting it for security, just to restrict where people key in information.

Any examples would be really appreciated also.

Thanks
Steve
 
I recomend code:

Sub Protect(myPassword As String)
ActiveWorkbook.ActiveSheet.Protect Password:=myPassword
ActiveWorkbook.Protect Password:=myPassword
End Sub


Sub UnProtect(myPassword As String)
ActiveWorkbook.ActiveSheet.Unprotect Password:=myPassword
ActiveWorkbook.Unprotect Password:=myPassword
End Sub

Then just call the macro:

Call Protect("here enter your password") or Call UnProtect("here enter your password")

This solution prevents opening UnProtecting macro from developer tab like could happen with Smitty solution.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Here you go:


Sub ProtectAll()
****Dim sh As Worksheet
****Dim myPassword As String
****myPassword = "password"
****
****For Each sh In ActiveWorkbook.Worksheets
********sh.Protect Password:=myPassword
****Next sh
****
End Sub

Sub UnrotectAll()
****Dim sh As Worksheet
****Dim myPassword As String
****myPassword = "password"
****
****For Each sh In ActiveWorkbook.Worksheets
********sh.Unprotect Password:=myPassword
****Next sh
****
End Sub


Hope that helps,

Smitty

what should i do in order to only protect Active sheet
 
Upvote 0

Forum statistics

Threads
1,224,144
Messages
6,176,647
Members
452,739
Latest member
SCEducator

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top