Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 573
Default Protect/unprotect worksheets

I've been working on this macro to protect and unprotect the worksheets
in a workbook I'm sending out to novice end users. Problem is,
sometimes it works, and sometimes it doesn't.


Sub AllSheetsToggleProtectWGrid()
'for all sheets in currently active workbook, assigned to button
'Password
Dim TopCell As Range
Dim TopCol As Range
Dim Cols2Hide As Range
Dim wkSht As Worksheet
Dim PWORD As String
Dim WksWasProtected As Boolean

PWORD = "dave"

Application.ScreenUpdating = False

For Each wkSht In ActiveWorkbook.Worksheets
If LCase(wkSht.Name) = LCase("County Records") Then
'do nothing
Else
With wkSht
wkSht.Select<-----VBA Error "Method Select of object
worksheet failed
If .ProtectContents Then
WksWasProtected = True
.Unprotect Password:=PWORD
ActiveWindow.DisplayGridlines = True
Else
wkSht.Select
WksWasProtected = False
ActiveWindow.DisplayGridlines = False
.Protect Password:=PWORD


If WksWasProtected Then
.Protect Password:=PWORD

End If
'End If
End If
End With
End If
Next wkSht

Application.ScreenUpdating = True

End Sub

It's failing at the wkSht.select command, which was working fine for a
while. I made some changes to some of the details of the formatting in
the target sheet, "County Records", that I didn't think would affect
this. Not sure what's going on. Does anyone have any ideas?
For clarification, the worksheet "County Records" does not get
protected, but all the other sheets do.
Thanks for the help!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Protect/unprotect worksheets

Just a hunch... Select will not work if there are hidden work sheets. I did
not look at all of your code but that is a good place to start.

HTH

"davegb" wrote:

I've been working on this macro to protect and unprotect the worksheets
in a workbook I'm sending out to novice end users. Problem is,
sometimes it works, and sometimes it doesn't.


Sub AllSheetsToggleProtectWGrid()
'for all sheets in currently active workbook, assigned to button
'Password
Dim TopCell As Range
Dim TopCol As Range
Dim Cols2Hide As Range
Dim wkSht As Worksheet
Dim PWORD As String
Dim WksWasProtected As Boolean

PWORD = "dave"

Application.ScreenUpdating = False

For Each wkSht In ActiveWorkbook.Worksheets
If LCase(wkSht.Name) = LCase("County Records") Then
'do nothing
Else
With wkSht
wkSht.Select<-----VBA Error "Method Select of object
worksheet failed
If .ProtectContents Then
WksWasProtected = True
.Unprotect Password:=PWORD
ActiveWindow.DisplayGridlines = True
Else
wkSht.Select
WksWasProtected = False
ActiveWindow.DisplayGridlines = False
.Protect Password:=PWORD


If WksWasProtected Then
.Protect Password:=PWORD

End If
'End If
End If
End With
End If
Next wkSht

Application.ScreenUpdating = True

End Sub

It's failing at the wkSht.select command, which was working fine for a
while. I made some changes to some of the details of the formatting in
the target sheet, "County Records", that I didn't think would affect
this. Not sure what's going on. Does anyone have any ideas?
For clarification, the worksheet "County Records" does not get
protected, but all the other sheets do.
Thanks for the help!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Protect/unprotect worksheets

I tried it out.

Change:
If LCase(wkSht.Name) = LCase("County Records") Then
'do nothing
Else

To:
If Not LCase(wkSht.Name) = "county records" And wkSht.Visible Then

Alternately, if you want to toggle the grids on the hidden worksheets
as well, you can set the wkSht.Visble property to True before the
changes and revert it back afterwards.

-- Nick

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
Protect and Unprotect all worksheets with macro Joe M. Excel Discussion (Misc queries) 2 January 22nd 10 09:26 PM
protect/unprotect grouped worksheets Todd Excel Programming 0 February 16th 05 04:31 PM
Protect/unprotect all worksheets Janna Excel Worksheet Functions 2 January 7th 05 01:01 AM
protect / unprotect worksheets using VBA ExcelUserinPA Excel Programming 2 November 1st 03 03:36 PM
Macro to protect/unprotect worksheets Cameron[_3_] Excel Programming 2 August 6th 03 10:31 PM


All times are GMT +1. The time now is 01:43 AM.

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"