Protecting worksheet

lbonsemb

New Member
Joined
Aug 11, 2002
Messages
18
If I protect a sheet through Tools/Protection, a button that runs a macro to hide rows cannot be executed without having to enter the password to unprotect it (which defeats the purpose of protecting the worksheet).
I know there is a way to protect through coding. Please give me a hand to solve this.
Thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Is this what you need...

'To protect the sheet place this at the bottom of your macro...
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True

'To unprotect the active sheet...place this at the top of you macro...


ActiveSheet.Unprotect

I hope this helps you.

David
 
Upvote 0
Is this what you need...

'To protect the sheet place this at the bottom of your macro...
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True

'To unprotect the active sheet...place this at the top of you macro...


ActiveSheet.Unprotect

I hope this helps you.

David
 
Upvote 0
Is this what you need...

'To protect the sheet place this at the bottom of your macro...
ActiveSheet.Protect DrawingObjects:=True,
Contents:=True, Scenarios:=True

'To unprotect the active sheet...place this at the top of you macro...


ActiveSheet.Unprotect

I hope this helps you.

David
 
Upvote 0
here is code to protect and unprotect a single worksheet through vba

To protect multiple sheets use For Each statement.

To protect single:

Sub Protect()
Dim mySheet As Worksheet
Set mySheet = Worksheets("Project WorkSheet")
mySheet.Protect "SIMONE", True, True
End Sub

To Unprotect single:

Sub UnProtect()
Dim mySheet As Worksheet
Set mySheet = Worksheets("Project WorkSheet")
mySheet.Unprotect "SIMONE"
End Sub

you can put this code behind a command button
or put it inline with your current code.

Hope this helps!

Dan
 
Upvote 0
here is code to protect and unprotect a single worksheet through vba

To protect multiple sheets use For Each statement.

To protect single:

Sub Protect()
Dim mySheet As Worksheet
Set mySheet = Worksheets("Project WorkSheet")
mySheet.Protect "SIMONE", True, True
End Sub

To Unprotect single:

Sub UnProtect()
Dim mySheet As Worksheet
Set mySheet = Worksheets("Project WorkSheet")
mySheet.Unprotect "SIMONE"
End Sub

you can put this code behind a command button
or put it inline with your current code.

Hope this helps!

Dan
 
Upvote 0

Forum statistics

Threads
1,225,053
Messages
6,182,582
Members
453,126
Latest member
NigelExcel

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