Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Password prtecting worksheets in code

I have a simple workbook with about 12 sheets - no other code, no macros, no
ranges. My code is at end of post. What I am trying to do is to automatically
unprotect the sheets when certain users open it (based on username) and have
all sheets protected when everone else opens it. Only 2 users on my network
are to have ability to edit any field on all worksheets. It works, but there
are a couple of anomalies.

I'm using unprotect code on Workbook_open and similar code to protect on
Workbook_BeforeClose. To try and solve why sometimes a sheet becomes
unprotected, I have also added this to Workbook_SheetActivate.

1) As I test after workbook opens with protection, I will choose the
unprotect from Tools menu on a sheet or 2 and then I move to another sheet
and all of a sudden, when I choose the file option, I do not get the password
dialog. This is random, as when I close book, open again and go to sheet that
was unprotected in my previous test, it is protected. Then I go to a couple
more sheets and it happens again. Sheets seem to become unprotected somehow
without using code or entering password.

2) Some fields in some of my worksheets don't have protection. Most do, but
as I randomly check fields, all of a sudden I come across one that just is
not protected. Again, there are no special features, other than auto
filtering.

I am running 2003 on XP.

Thanks,
Clint

MY CODE:

Dim wsSheet As Worksheet
Const PWORD As String = "testing"

If Environ("UserName") = "cherman" Then
For Each wsSheet In Worksheets
wsSheet.Protect Password:=PWORD
Next wsSheet
Else
For Each wsSheet In Worksheets
wsSheet.Unprotect
Next wsSheet
End If

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Password prtecting worksheets in code


Hello Cherman,

You maybe experiencing a timing problem. Code execution and othe
operations are not always synchronous. The For Each may be the proble
here. This method of looping is much faster than the old trusty Fo
Next loop. You maybe loop faster than VBA can unprotect the shee
causing the anomolies. Try inserting a delay in your loop or recode i
with a For Next loop

Sincerely,
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=47838

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
remove password on VBA code Tom Excel Discussion (Misc queries) 1 February 13th 10 06:36 AM
How to see macro code of a password protected macro without a password? Dmitry Kopnichev Excel Worksheet Functions 5 October 27th 05 09:57 AM
code asking for password Jonsson[_30_] Excel Programming 3 May 13th 04 09:47 AM
VBA code delete code but ask for password and unlock VBA protection WashoeJeff Excel Programming 0 January 27th 04 07:07 AM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM


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

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

About Us

"It's about Microsoft Excel"