0% found this document useful (0 votes)
26 views6 pages

172 Protecting Specific Cells and Data Validation

The document describes validation routines for data entered into a spreadsheet. It includes macros to lock and unlock cells, add formulas, check for errors, and protect the sheet. The validation checks for correct data types in various columns and highlights errors in red.

Uploaded by

Juaquim Aredidon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

172 Protecting Specific Cells and Data Validation

The document describes validation routines for data entered into a spreadsheet. It includes macros to lock and unlock cells, add formulas, check for errors, and protect the sheet. The validation checks for correct data types in various columns and highlights errors in red.

Uploaded by

Juaquim Aredidon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 6

General Title

Hidden Info - To be protected


Title 1 - Data Validation Title 4 - Data Validation
Title 2 Title 3 Title 5 Title 6 Title 7
from list from drop down list
1001 - Project 1 Date Date Free text Number Number
Title 8 Title 9 Title 10 Title 11 Title 12 Title 13 Title 14 Title 15 Title 16 Title 17

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"

'Removed any locked protection from first blank row


Range("A1048576").End(xlUp).Offset(1, 0).EntireRow.Locked = False

'Find the second blank row


X = Range("A1048576").End(xlUp).Offset(2, 0).Row

'Lock "blank" rows except the next free one


'A:Q
Range("A" & X & ":Q1048576").Locked = True

'Lock the formulas


'Column K
Range("K" & X - 1).Locked = True
'Column P
Range("P" & X - 1).Locked = True

'Go to first populatable cell


Range("A1048576").End(xlUp).Offset(1, 0).Select

'Protect sheet
ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub
Sub ValidateMe()
'A simple data validation routine

Dim ErrorCheck As Integer 'Catpures the count of errors

'Unprotect sheet
ActiveSheet.Unprotect Password:="password"

'Go to first populatable cell


Range("A1048576").End(xlUp).Select

'Remove any "error" highlights


ActiveCell.EntireRow.Interior.Pattern = xlNone

'The validation routine


'Column A (text)...repeat for any columns with text (by increasing the offset)
If Len(ActiveCell.Value) = "" Then
'Colour the cell red
ActiveCell.Interior.Color = vbRed
'Increase error counter by 1
ErrorCheck = ErrorCheck + 1
End If

'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

'Check to see if there were any errors


If ErrorCheck <> 0 Then
'Display error message
MsgBox "Please check your input and correct any errors (in red)", vbOKOnly, "Errors in data type detected"
'Reprotect the sheet
ActiveSheet.Protect Password:="password", _
DrawingObjects:=True, Contents:=True, Scenarios:=True
'End here
End
End If

Call ProtectMe

End Sub
Sub ProtectMe()

Dim X As Integer

'Add your formulas for K and P here


'ActiveCell.Offset(0, 10).Formula = Whatever it is
'ActiveCell.Offset(0, 15).Formula = Whatever it is

'Lock all of the row just validated


ActiveCell.EntireRow.Locked = True
'Unlock all of the following row
ActiveCell.Offset(1, 0).EntireRow.Locked = False

'Find the second blank row


X = Range("A1048576").End(xlUp).Offset(2, 0).Row

'Lock "blank" rows except the next free one


'A:Q
Range("A" & X & ":J1048576").Locked = True

'Lock the formulas


'Column K
Range("K" & X - 1).Locked = True
'Column P
Range("P" & X - 1).Locked = True

'Go to first populatable cell


X = Range("A" & X - 1).Select

'Protect sheet
ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub
pe detected"

You might also like