PC Review


Reply
Thread Tools Rate Thread

bug in protect-unprotect macro

 
 
stef
Guest
Posts: n/a
 
      9th Oct 2007
Excel 2002 SP3
Win XP Pro

*Follow-up to: microsoft.public.excel.programming*

Hi,

I have a bug with the line ".EnableSelection = xlUnlockedCells" in this
macro:

Sub ProtectAllSheets()
Application.ScreenUpdating = False
Dim n As Single
For n = 1 To Sheets.Count
With Sheets(n)
.Protect Password:="nosecret"
.EnableSelection = xlUnlockedCells
End With
Next n
Application.ScreenUpdating = True
End Sub

It's always worked in the past and I can't understand what is happening now.

Be easy on me as I am a newbie at XL macros/VBA....
 
Reply With Quote
 
 
 
 
Gord Dibben
Guest
Posts: n/a
 
      9th Oct 2007
The "bug" shows itself in what manner?


Gord Dibben MS Excel MVP

On Tue, 09 Oct 2007 14:43:39 -0400, stef <(E-Mail Removed)> wrote:

>Excel 2002 SP3
>Win XP Pro
>
>*Follow-up to: microsoft.public.excel.programming*
>
>Hi,
>
>I have a bug with the line ".EnableSelection = xlUnlockedCells" in this
>macro:
>
>Sub ProtectAllSheets()
> Application.ScreenUpdating = False
> Dim n As Single
> For n = 1 To Sheets.Count
> With Sheets(n)
> .Protect Password:="nosecret"
> .EnableSelection = xlUnlockedCells
> End With
> Next n
> Application.ScreenUpdating = True
>End Sub
>
>It's always worked in the past and I can't understand what is happening now.
>
>Be easy on me as I am a newbie at XL macros/VBA....


 
Reply With Quote
 
stef
Guest
Posts: n/a
 
      9th Oct 2007
Error "438':
Object doesn't support this property or method

I should have posted that, sorry.

Gord Dibben wrote:
> The "bug" shows itself in what manner?
>
>
> Gord Dibben MS Excel MVP
>
> On Tue, 09 Oct 2007 14:43:39 -0400, stef <(E-Mail Removed)> wrote:
>
>> Excel 2002 SP3
>> Win XP Pro
>>
>> *Follow-up to: microsoft.public.excel.programming*
>>
>> Hi,
>>
>> I have a bug with the line ".EnableSelection = xlUnlockedCells" in this
>> macro:
>>
>> Sub ProtectAllSheets()
>> Application.ScreenUpdating = False
>> Dim n As Single
>> For n = 1 To Sheets.Count
>> With Sheets(n)
>> .Protect Password:="nosecret"
>> .EnableSelection = xlUnlockedCells
>> End With
>> Next n
>> Application.ScreenUpdating = True
>> End Sub
>>
>> It's always worked in the past and I can't understand what is happening now.
>>
>> Be easy on me as I am a newbie at XL macros/VBA....

>

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      9th Oct 2007
Do you have any chart sheets or such? If so then you will get the error you
discussed. Try this to see if it goes any better...

Sub ProtectAllSheets()
Dim wks As Worksheet

Application.ScreenUpdating = False
For Each wks In Worksheets
With wks
.Protect Password:="nosecret"
.EnableSelection = xlUnlockedCells
End With
Next wks
Application.ScreenUpdating = True
End Sub
--
HTH...

Jim Thomlinson


"stef" wrote:

> Error "438':
> Object doesn't support this property or method
>
> I should have posted that, sorry.
>
> Gord Dibben wrote:
> > The "bug" shows itself in what manner?
> >
> >
> > Gord Dibben MS Excel MVP
> >
> > On Tue, 09 Oct 2007 14:43:39 -0400, stef <(E-Mail Removed)> wrote:
> >
> >> Excel 2002 SP3
> >> Win XP Pro
> >>
> >> *Follow-up to: microsoft.public.excel.programming*
> >>
> >> Hi,
> >>
> >> I have a bug with the line ".EnableSelection = xlUnlockedCells" in this
> >> macro:
> >>
> >> Sub ProtectAllSheets()
> >> Application.ScreenUpdating = False
> >> Dim n As Single
> >> For n = 1 To Sheets.Count
> >> With Sheets(n)
> >> .Protect Password:="nosecret"
> >> .EnableSelection = xlUnlockedCells
> >> End With
> >> Next n
> >> Application.ScreenUpdating = True
> >> End Sub
> >>
> >> It's always worked in the past and I can't understand what is happening now.
> >>
> >> Be easy on me as I am a newbie at XL macros/VBA....

> >

>

 
Reply With Quote
 
stef
Guest
Posts: n/a
 
      9th Oct 2007
Jim,
You are totally correct.
That must be the problem; I do have charts....
Let me try your version and post back here. Thanks.

Jim Thomlinson wrote:
> Do you have any chart sheets or such? If so then you will get the error you
> discussed. Try this to see if it goes any better...
>
> Sub ProtectAllSheets()
> Dim wks As Worksheet
>
> Application.ScreenUpdating = False
> For Each wks In Worksheets
> With wks
> .Protect Password:="nosecret"
> .EnableSelection = xlUnlockedCells
> End With
> Next wks
> Application.ScreenUpdating = True
> End Sub

 
Reply With Quote
 
stef
Guest
Posts: n/a
 
      9th Oct 2007
It does work perfectly.

What about undoing it--the un-protect part:

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

Now becomes:

Sub UnprotectAllSheets()
Application.ScreenUpdating = False
For Each wks In Worksheets
With wks
Sheets(n).Unprotect Password:="nosecret"
End With
Next wks
Application.ScreenUpdating = True
End Sub

*Is that correct?*


Jim Thomlinson wrote:
> Do you have any chart sheets or such? If so then you will get the error you
> discussed. Try this to see if it goes any better...
>
> Sub ProtectAllSheets()
> Dim wks As Worksheet
>
> Application.ScreenUpdating = False
> For Each wks In Worksheets
> With wks
> .Protect Password:="nosecret"
> .EnableSelection = xlUnlockedCells
> End With
> Next wks
> Application.ScreenUpdating = True
> End Sub

 
Reply With Quote
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      10th Oct 2007
I did tweak it a bit, but didn't have problems where you did.

Sub ProtectAllSheets()
Dim WS As Worksheet
Application.ScreenUpdating = False
For Each WS In ThisWorkbook.Worksheets

With WS
.Protect Password:="nosecret"
.EnableSelection = xlUnlockedCells
End With
Next WS
Application.ScreenUpdating = True
End Sub

--
HTH,
Barb Reinhardt



"stef" wrote:

> Excel 2002 SP3
> Win XP Pro
>
> *Follow-up to: microsoft.public.excel.programming*
>
> Hi,
>
> I have a bug with the line ".EnableSelection = xlUnlockedCells" in this
> macro:
>
> Sub ProtectAllSheets()
> Application.ScreenUpdating = False
> Dim n As Single
> For n = 1 To Sheets.Count
> With Sheets(n)
> .Protect Password:="nosecret"
> .EnableSelection = xlUnlockedCells
> End With
> Next n
> Application.ScreenUpdating = True
> End Sub
>
> It's always worked in the past and I can't understand what is happening now.
>
> Be easy on me as I am a newbie at XL macros/VBA....
>

 
Reply With Quote
 
stef
Guest
Posts: n/a
 
      10th Oct 2007
Bard, same as the one Jim posted and I thank both very much for it--it
does work and makes my life easier.

Barb Reinhardt wrote:
> I did tweak it a bit, but didn't have problems where you did.
>
> Sub ProtectAllSheets()
> Dim WS As Worksheet
> Application.ScreenUpdating = False
> For Each WS In ThisWorkbook.Worksheets
>
> With WS
> .Protect Password:="nosecret"
> .EnableSelection = xlUnlockedCells
> End With
> Next WS
> Application.ScreenUpdating = True
> End Sub
>

 
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
bug in protect-unprotect macro stef Microsoft Excel Discussion 2 10th Oct 2007 02:01 AM
unprotect and protect within a macro Staalkop Microsoft Excel Discussion 4 27th Oct 2005 08:44 PM
Protect/Unprotect Sheet from Macro Nigel Microsoft Excel Programming 1 14th May 2004 05:55 PM
Macro to protect and unprotect tetreaultl Microsoft Excel Programming 1 1st Apr 2004 06:16 PM
Macro to protect/unprotect worksheets Cameron Microsoft Excel Programming 2 6th Aug 2003 10:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:14 AM.