odartey
Member
Hello All,
I got this code which is working very very well.
But i want to edit the code such that it works from the worksheet instead of This workbook where the locking applies to all active sheets.
What i want the code to achieve is to apply it to just two of the worksheets in the workbook than to all of the active sheets in the workbook.
ie, if i have 30 active worksheets in the workbook, it should apply just to TWO of the worksheets.
Any help please ..............
Kind Regards
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
'Resume to next line if any error occurs
Dim Cell As Range
With ActiveSheet
'first of all unprotect the entire
'sheet and unlock all cells
.Unprotect Password:="xx"
.Cells.Locked = False
'Now search for non blank cells
'and lock them and unlock blank cells
For Each Cell In ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:="xx"
'Protect with blank password, you can change it
End With
Exit Sub
End Sub
I got this code which is working very very well.
But i want to edit the code such that it works from the worksheet instead of This workbook where the locking applies to all active sheets.
What i want the code to achieve is to apply it to just two of the worksheets in the workbook than to all of the active sheets in the workbook.
ie, if i have 30 active worksheets in the workbook, it should apply just to TWO of the worksheets.
Any help please ..............
Kind Regards
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
'Resume to next line if any error occurs
Dim Cell As Range
With ActiveSheet
'first of all unprotect the entire
'sheet and unlock all cells
.Unprotect Password:="xx"
.Cells.Locked = False
'Now search for non blank cells
'and lock them and unlock blank cells
For Each Cell In ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:="xx"
'Protect with blank password, you can change it
End With
Exit Sub
End Sub