172 Protecting Specific Cells and Data Validation
172 Protecting Specific Cells and Data Validation
Date Date Date Formula Number Text Date Date Formula Text
Private Sub Workbook_Open()
'Assumes only 1 sheet
'Locks all cells except the ones the user can use
Dim X As Integer 'Holds the row number of the first row to lock
'Unprotect sheet
ActiveSheet.Unprotect Password:="password"
'Protect sheet
ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Sub ValidateMe()
'A simple data validation routine
'Unprotect sheet
ActiveSheet.Unprotect Password:="password"
'Column B (date)...repeat for any columns with dates (by increasing the offset)
If IsDate(ActiveCell.Offset(0, 1).Value) = False Then
'Colour the cell red
ActiveCell.Offset(0, 1).Interior.Color = vbRed
'Increase error counter by 1
ErrorCheck = ErrorCheck + 1
End If
'Column F (number)...repeat for any columns with numbers (by increasing the offset)
If IsNumeric(ActiveCell.Offset(0, 5).Value) = False Then
'Colour the cell red
ActiveCell.Offset(0, 5).Interior.Color = vbRed
'Increase error counter by 1
ErrorCheck = ErrorCheck + 1
End If
Call ProtectMe
End Sub
Sub ProtectMe()
Dim X As Integer
'Protect sheet
ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
pe detected"