Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) If ActiveSheet.Name = "Sheet1" Then 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:="123" .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 = "123" Then Cell.Locked = False Else Cell.Locked = True End If Next Cell .Protect Password:="123" 'Protect with blank password, you can change it End With Exit Sub End If End Sub 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:="123" .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 = "123" Then Cell.Locked = False Else Cell.Locked = True End If Next Cell .Protect Password:="123" 'Protect with blank password, you can change it End With Exit Sub End Sub