Macro to unprotect workbook

M

Mike

Hi

I have the following macros coded to protect and unprotect a workbook I'm
working on. However, when I try to unprotect the workbook using
UnprotectAllSheets(), it keeps failing, particularly with reference to the
part ".Cells.FormulaHidden = False". This had worked fine for a while, but
now that other macros have been put into the workbook, it now seems to fail.

Has anybody any thoughts on what might be causing this? Thanks in advance
for any help.

Mike


Sub UnprotectAllSheets()

Application.ScreenUpdating = False
Dim n As Single
For n = 1 To Sheets.Count
With Sheets(n)
.Unprotect Password:="password"
.Cells.FormulaHidden = False
End With
Next n
Application.ScreenUpdating = True

End Sub


Sub ProtectAllSheets()

Application.ScreenUpdating = False
Dim n As Single
For n = 1 To Sheets.Count
Sheets(n).Protect Password:="password"

Next n
Application.ScreenUpdating = True

End Sub


Sub HideFormulaCode()

Application.ScreenUpdating = False
Dim n As Single
For n = 1 To Sheets.Count
With Sheets(n)
.Cells.FormulaHidden = True
.Protect Password:="password"

End With
Next n
Application.ScreenUpdating = True

End Sub
 
R

Ron de Bruin

Yes

You use sheets in your loop
There are no cells in a chart sheet that's why your code blow.

Change sheets to worksheets in the code(2*) and it will work for all the worksheets
 
M

Mike

That now works fine.

Cheers for the help - I was sure that it was something quick and easy to
correct.

Mike
 
T

Tom Ogilvy

Do you have merged cells on any of the sheets. Just a guess, but that can
sometimes interfere when you are changing properties.
 
M

Mike

That now works fine.

Cheers for the help - I was sure that it was something quick and easy to
correct.

Mike
 
M

Mike

cheers

Ron de Bruin said:
Yes

You use sheets in your loop
There are no cells in a chart sheet that's why your code blow.

Change sheets to worksheets in the code(2*) and it will work for all the worksheets
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top