Macro to protect Cells on all sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,784
Office Version
  1. 2019
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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

fadee2

Active Member
Joined
Nov 7, 2020
Messages
337
Office Version
  1. 2019
Platform
  1. Windows
.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...
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
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.
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,784
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

thanks for your input. How do I overcome this in my code ?
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
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
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
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
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,784
Office Version
  1. 2019
Platform
  1. Windows
Many thanks for the help
 

Watch MrExcel Video

Forum statistics

Threads
1,130,042
Messages
5,639,731
Members
417,108
Latest member
Thein Than

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
Top