PC Review


Reply
Thread Tools Rate Thread

Code to Lock and Unlock a sheet

 
 
Drew
Guest
Posts: n/a
 
      15th Oct 2009
I'm sure that this is very basic code, but I am a newby to VBA. Currently I
have some code that is running that will update my pivot table when I click
on the tab where the pivot table resides (Sales). However, when I protect
this sheet, the code fails and I get an error. Can someone help me with the
code that I need to unprotect the sheet prior to the code that runs the pivot
table update and protect the sheet once the code is done running.

Thanks,
--
Drew
 
Reply With Quote
 
 
 
 
B Lynn B
Guest
Posts: n/a
 
      15th Oct 2009
Using unprotect and reprotect creates risk of sheets being left unprotected
in the event of runtime errors unless you take considerable care in your
error handling.

Simpler solution is to protect with the property UserInterfaceOnly set to
true. e.g...

ActiveSheet.protect "password", UserInterfaceOnly:=True

This allows any running VBA code to make changes while the user cannot do so
directly. Items of note when using this method - it is not retained after
the workbook is closed, so you may want to include it in the workbook open
event. And it's occasionally a little quirky about not allowing certain
kinds of changes, (Copy and paste a range from unprotected source to
protected sheet - go figure).


"Drew" wrote:

> I'm sure that this is very basic code, but I am a newby to VBA. Currently I
> have some code that is running that will update my pivot table when I click
> on the tab where the pivot table resides (Sales). However, when I protect
> this sheet, the code fails and I get an error. Can someone help me with the
> code that I need to unprotect the sheet prior to the code that runs the pivot
> table update and protect the sheet once the code is done running.
>
> Thanks,
> --
> Drew

 
Reply With Quote
 
Gord Dibben
Guest
Posts: n/a
 
      15th Oct 2009
Private Sub Worksheet_Activate()
Me.Unprotect Password:="drowssap"
'do your stuff code
Me.Protect Password:="drowssap"
End Sub


Gord Dibben MS Excel MVP

On Thu, 15 Oct 2009 09:23:45 -0700, Drew <(E-Mail Removed)> wrote:

>I'm sure that this is very basic code, but I am a newby to VBA. Currently I
>have some code that is running that will update my pivot table when I click
>on the tab where the pivot table resides (Sales). However, when I protect
>this sheet, the code fails and I get an error. Can someone help me with the
>code that I need to unprotect the sheet prior to the code that runs the pivot
>table update and protect the sheet once the code is done running.
>
>Thanks,


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to unlock and lock cells in password protected sheet Chris Microsoft Excel Misc 3 26th Feb 2010 09:06 PM
I FORGOT MY PASSWORD AND THE EXCEL SHEET IS LOCK HOW CAN I UNLOCK =?Utf-8?B?QXNhZg==?= Microsoft Excel Misc 1 4th Oct 2006 02:50 AM
i want to lock a tab in excel, and unlock in another sheet =?Utf-8?B?am9obiB0ZW1wZXN0?= Microsoft Excel Programming 0 18th Nov 2005 05:06 PM
How do I lock the work sheet and then unlock specific cells? =?Utf-8?B?YnBhcnRpbmdAc3RhbmRhcmQuY29t?= Microsoft Excel Misc 2 23rd Apr 2004 11:55 PM
Dynamically lock / unlock a cell in protected sheet ripa Microsoft Excel Misc 1 18th Nov 2003 01:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:06 PM.