Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Protecting Sheet

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nathan_savidge - 23 May 2008 11:34 GMT
Hi,

I have recently migrated data from one excel sheet to another.  However it
is now required to protect all the migrated sheets.

I wish to do this via code rather than go into each sheet and protect.

I have opened each sheet with code and used the following code :

xl.ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
           , AllowFormattingCells:=True, AllowFormattingRows:=True

However, i can not put the password in, this just protects the sheet with no
password, is there a way of adding the password to the code?

Thanks

Nathan.
Mike H - 23 May 2008 12:17 GMT
Try

Sub marine()
   Dim ws As Worksheet
   For Each ws In ThisWorkbook.Worksheets
       ws.Protect Password:="MyPass", DrawingObjects:=True, Contents:=True,
Scenarios:=True _
           , AllowFormattingCells:=True, AllowFormattingRows:=True

   Next ws
End Sub

Mike

> Hi,
>
[quoted text clipped - 14 lines]
>
> Nathan.
Gord Dibben - 24 May 2008 01:12 GMT
Sub ProtectAllSheets()
   Application.ScreenUpdating = False
   Dim n As Single
   For n = 1 To Sheets.Count
       Sheets(n).Protect Password:="justme", DrawingObjects:=True, _
       Contents:=True, Scenarios:=True _
           , AllowFormattingCells:=True, AllowFormattingRows:=True
   Next n
   Application.ScreenUpdating = True
End Sub

Sub UnprotectAllSheets()
   Application.ScreenUpdating = False
   Dim n As Single
   For n = 1 To Sheets.Count
       Sheets(n).Unprotect Password:="justme"
   Next n
   Application.ScreenUpdating = True
End Sub

Gord Dibben  MS Excel MVP

>Hi,
>
[quoted text clipped - 14 lines]
>
>Nathan.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.