Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Protecting/Unprotecting Workbook

I'd like to be able to protect a workbook from changes via VBA. In Word,
I have these two bits of code...

Sub LockItUp(doit)
If doit < wdNoProtection Then
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=doit, NoReset:=True,
Password:="myPass1234"
End If
End If
End Sub

Function UnlockIt()
Dim wasLocked
wasLocked = ActiveDocument.ProtectionType
If ActiveDocument.ProtectionType < wdNoProtection Then
ActiveDocument.Unprotect "myPass1234"
End If
UnlockIt = wasLocked
End Function

The function UnLockIt() simply locks up a document with a password and
returns what the prior protection scheme was. LockItUp() simply locks it
back up with whatever parameter it is given. I usually run these during
save, close, open, etc...

I generally know now how to do it in Excel, but how do I do it most
effectively? :)

Thanks!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Protecting/Unprotecting Workbook

Sub LockIT()
If activeworkbook.protectcontents = False then activeworkbook.protect "mypass"
end Sub

Sub UnlockIT()
if activeworkbook.protectcontents = true then activeworkbook.unprotect "mypass"
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Protecting/Unprotecting Workbook

Steven Revell wrote:
Sub LockIT()
If activeworkbook.protectcontents = False then activeworkbook.protect "mypass"
end Sub

Sub UnlockIT()
if activeworkbook.protectcontents = true then activeworkbook.unprotect "mypass"
End Sub


Thanks, Steven.

When I go to "unprotect" the workbook, will it respect the "locked"
cells I have on the worksheet?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Protecting/Unprotecting Workbook

I'm not sure what you mean by respect.

Cells will only be locked if the worksheet is locked:
activesheet.protect "mypass"

if a worksheet is not locked then cells will not be locked even if their properties say they are.

sub LockSheet
if activesheet.protectcontents = false then activesheet.protect "mypass"
end sub

sub UnLockSheet
if activesheet.protectcontents = true then activesheet.unprotect "mypass"
end sub

sub LockWorkbook
if activeworkbook.protectstructure = false then activeworkbook.protect "mypass"
end sub

sub UnLockWorkbook
if activeworkbook.protectstructure = True then activeworkbook.unprotect "mypass"
end sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Protecting/Unprotecting Workbook

Steven Revell wrote:

I'm not sure what you mean by respect.

Cells will only be locked if the worksheet is locked:
activesheet.protect "mypass"

if a worksheet is not locked then cells will not be locked even if their properties say they are.


Let me explain what I'm trying to do more clearly: I want to toggle
between two worksheet states...

1. all cells locked from use (total protection)
2. some cells locked from use (partial protection)

I'm thinking I'll have to somehow hardcode the locked cells references,
but I don't want to have to do that! :)



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Protecting and Unprotecting several worksheets at one time Learning Excel Excel Discussion (Misc queries) 8 February 4th 09 10:07 PM
protecting and unprotecting Fusion1337 Excel Worksheet Functions 5 September 26th 08 04:03 PM
PROTECTING/UNPROTECTING SHEETS Maureen Excel Discussion (Misc queries) 1 January 6th 05 06:46 PM
Protecting and Unprotecting multiple sheets pkley Excel Programming 3 January 22nd 04 05:26 PM
Protecting & unprotecting worksheets pkley Excel Programming 1 January 7th 04 10:28 PM


All times are GMT +1. The time now is 11:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"