I am fairly new to the VBA world, so any assistance would be greatly appreciated!
I have a worksheet which is intended for user input and contains a specific column with data validation drop down lists. I have code that prevents the user from pasting into this specific column and forces them to use the drop down selection. I also want to protect the worksheet with a password "secret". When I protect the sheet, the code that prevents the pasting into the data validation no longer works. I have tried the code below and the worksheet remains protected however it still allows me to paste over the data validation. It is as if the two are mutually exclusive. Any ideas?
Sub unprotectworksheet()
Sheets("P24").Unprotect Password:="secret"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("ValidationRange")) Then
Exit Sub
Else
Application.Undo
MsgBox "You can not paste in this cell. " & _
"It would have deleted data validation rules. ", vbCritical
End If
End Sub
Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
x = r.Validation.Type
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
Sub protectworksheet()
Sheets("P24").Protect Password:="secret"
End Sub
I have a worksheet which is intended for user input and contains a specific column with data validation drop down lists. I have code that prevents the user from pasting into this specific column and forces them to use the drop down selection. I also want to protect the worksheet with a password "secret". When I protect the sheet, the code that prevents the pasting into the data validation no longer works. I have tried the code below and the worksheet remains protected however it still allows me to paste over the data validation. It is as if the two are mutually exclusive. Any ideas?
Sub unprotectworksheet()
Sheets("P24").Unprotect Password:="secret"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("ValidationRange")) Then
Exit Sub
Else
Application.Undo
MsgBox "You can not paste in this cell. " & _
"It would have deleted data validation rules. ", vbCritical
End If
End Sub
Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
x = r.Validation.Type
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
Sub protectworksheet()
Sheets("P24").Protect Password:="secret"
End Sub