VBA to unprotect & protect some sheets not all.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I've had a look online but all I seem to find is a code to unprotect all the sheets in my workbook.

I have a workbook with 5 sheets and I was wondering how I could unprotect/protect just 3 of the sheets instead of unprotecting all 5?

The sheets I would like unprotecting/protecting are called:
PER_SHIFT
DATA
WEEKLY

Thanks

Dan
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You could use something like

VBA Code:
Private Sub Protect_Click()
    
    Sheets("PER_SHIFT").Protect
    Sheets("DATA").Protect
    Sheets("WEEKLY").Protect

End Sub
 
Upvote 0
Hi Diving_Dan,

Thank you for your reply.

Sorry I forgot to mention that my sheets are password protected.

I have just managed to put this code together after searching online and it works.

VBA Code:
Sub Unprotect_3_Sheets()
Dim SheetList As Variant
SheetList = Array("DATA", "PER_SHIFT", "WEEKLY")
For Each SheetList In ActiveWorkbook.Worksheets
SheetList.Unprotect Password:="motorola"
SheetList.Application.EnableEvents = False
Next SheetList
End Sub

Thanks
Dan
 
Upvote 0
Hi,
try following

VBA Code:
Sub ProtectSheets(ByVal ProtectSheet As Boolean, Optional Password As String)
 Dim sh As Worksheet
 For Each sh In Worksheets(Array("PER_SHIFT", "Data", "WEEKLY"))
    If ProtectSheet Then sh.Protect Password:=Password Else sh.Unprotect Password:=Password
 Next sh
End Sub

and to call it

VBA Code:
'protect
ProtectSheets True, "Dan"

'unprotect
ProtectSheets False, "Dan"

Dave
 
Upvote 0

Forum statistics

Threads
1,214,381
Messages
6,119,192
Members
448,874
Latest member
Lancelots

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