I have a (simplified example) matrix consisting of inputs and alarms. Each action (X) should have an input and an alarm i.e. no actions should be inserted in column E or row 6.
Capture.PNG
I used data validation to implement this and it works.
Capture2.PNG
However if I paste data to these cells they do not follow the validation rules. I inserted this VBA code to prevent this (extracted from www.j-walk.com/ss/excel/tips/tip98.htm):
Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("ValidationRange")) Then
Exit Sub
Else
Application.EnableEvents = False
Application.Undo
MsgBox "Your last operation was canceled." & _
" 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
However, this code also prevents values from being pasted into cells even when they do not break validation rules e.g. if I paste an X to input a;alarm 1, I get an error message. Is there any way to make the make the user aware if data he/she has pasted has broken data validation rules. This can be through error/warning message or highlighting of invalid cells, whichever is possible.
Bookmarks