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
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