Macro to protect Cells on all sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to protect cells on all sheets

However when running the macro, I get a run time error "no cells were found" and the code below

Code:
 .Cells.SpecialCells(xlCellTypeFormulas).Locked = True


It would be appreciated if someone could kindly assist me


Code:
Sub ProtectAll()
   Dim i As Long
   For i = 1 To Sheets.Count

      With Sheets(i)
.Unprotect
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Protect AllowDeletingRows:=True
.Protect AllowInsertingColumns:=True
.Protect AllowFormattingColumns:=True
.Protect AllowFiltering:=True

End With
 Next i
With ActiveSheet
.Select
End With

ActiveSheet.Select
 
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True

The piece of code looks for cells containing formula and locks them. check to see if the worksheet in question contains cells with formulas.
The error denotes the procedure is unable to find any cells with formulas.

hth...
 
Upvote 0
When new workbook is created, you can see each cell under Format>Protection has Locked ticked.

Once protection is activated, every cell is locked and you cannot do anything to it.

You have error because
.Cells.Locked = False
cleared the locking tick marks, thus
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
cannot find any ticked formulated cell.
 
Upvote 0
Never mind. I think I misunderstood what you were trying to do :(

You looped through all sheets. Most likely one of the sheet has no formula on it, thus causing error.
 
Upvote 0
thanks for your input. How do I overcome this in my code ?
 
Upvote 0
thanks for your input. How do I overcome this in my code ?
Maybe just put On Error Resume Next?

This way when sheet without formula is found, error is ignored.

Do not forget to put On Error GoTo 0
 
Upvote 0
Perhaps like this
VBA Code:
Sub ProtectAll()

Dim i As Long

On Error Resume Next
For i = 1 To Sheets.Count
    With Sheets(i)
        .Unprotect
        .Cells.Locked = False
        .Cells.SpecialCells(xlCellTypeFormulas).Locked = True
        .Protect AllowDeletingRows:=True
        .Protect AllowInsertingColumns:=True
        .Protect AllowFormattingColumns:=True
        .Protect AllowFiltering:=True
    End With
Next i
On Error GoTo 0
With ActiveSheet
    .Select
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top