Home » excel » excel – With Copy Paste over Data Validation cell , its not working

excel – With Copy Paste over Data Validation cell , its not working

Posted by: admin April 23, 2020 Leave a comment


I set up a excel sheet with drop-down menus for some of the cells. The
user can only select values from that list and an error message pops up
when something is typed it that is not in the list (via Data Validation

Error Alert).

So this works all fine … But when the user copy paste into the cells then validation doesnt work. How to make validation effective in case of copy paste.
I have searched and found one solution but its not working.
Here is the code that I have found. but its not working any more..It always return true enven I copy paste worng

Private Sub Worksheet_Change(ByVal Target As Range)

   If HasValidation(Range(ActiveCell.Address)) Then
        Exit Sub
        MsgBox "Your last operation was canceled." & _
        "It would have deleted data validation rules.", vbCritical

    End If
End Sub

Private Function HasValidation(r) As Boolean
    On Error Resume Next
    x = r.Validation.Type
    If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
How to&Answers:

It looks like you took the code from this page:


When Worksheet_Change fires, your code always refers to the active cell, not the target where the copy/paste operation is being performed.





and see if that works.


The “Target.Address” worked for me as mentioned by @JimmyPena.
But the solution still brings a bug, that if one tries to add another validation after adding the above mentioned VB code, the user is fired with the amount of message boxes produced by the vb code which is applied to the number of cells (viz. You will have to click on “OK” of the message box provided by the VB code for the number of cells the VB code has been applied to.. If code applied to 40 cells then you have to click “OK” 40 times… phew..)
Can you please try to help to add another condition to help this?
Or the last way that remains to only add the VB code after adding all the validations.