Error: Lock cells with data when saved

loni

New member
Joined
Jan 7, 2019
Messages
1
Reaction score
0
Points
0
Excel Version(s)
Macro-enabled worksheet
Hi all,

I have already used this VBA code to lock cells with data in them when the sheet is saved:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'What is the password for sheet?
Const myPass As String = "Password"
Dim ws As Worksheet

'Loop through all worksheets
For Each ws In ThisWorkbook.Worksheets
With ws
'Unprotect from previous state
.Unprotect myPass
'Lock any cells that have data in them
.Range("A:T").SpecialCells(xlCellTypeConstants).Locked = True
.Range("A:T").SpecialCells(xlCellTypeFormulas).Locked = True
'Reprotect the sheet
.Protect myPass
End With
Next ws
End Sub

However, this function has stopped working and returns an 1004 error message saying "no cells found".
What am I doing wrong? :confused2:

- Loni
 
Hi and welcome
When posting code, please wrap it with code tags ( Edit code - select code - click the #button.)
It keeps the macro's structure and makes it easy to copy and handle.
Thank you
 
It would also be useful to add your XL version to your profile instead of " macro enabled worksheet". Thanks
 
Back
Top