protect data validation to fail while copy and paste data

Private Sub Worksheet_Change(ByVal Target As Range)
    'Does the validation range still have validation?
    Set range1 = Range("B2:B6500")
    If HasValidation(range1) Then
        Exit Sub
    Else
        Application.Undo
        MsgBox "您的操作將會被取消, " & vbCrLf & "請使用下拉選單進行選擇", 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

source: Ensuring That Data Validation Is Not Deleted

This entry was posted in Excel. Bookmark the permalink.