Help Needed for Drop Down Validation

NonStopLeo

New Member
Joined
Mar 25, 2010
Messages
38
Friends, I need a little help, if you can please do.
In below link, I have attached my sheet. In Column E, i have given a drop down list from where any value can be pasted. but whenever I copy some value from other sheet and paste, it is also accepting values other than listed in drop down list. I need some validation, that the value should be selected from drop down list only, if wrong value is pasted which is not in list, should not be accepted, only listed value should be accepted. thanks.

https://jumpshare.com/v/aXxA2jvq9ctYgvRmf7sb
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macros into the empty code window that opens up. Change the range in the code to suit your needs. Close the code window to return to your sheet.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If HasValidation(Range("E2:E5")) Then
        Exit Sub
    Else
        Application.Undo
        MsgBox "Error: You cannot paste data into these cells." & _
        "Please select from the drop-down list.", vbCritical
        Application.CutCopyMode = False
    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
 
Last edited:
Upvote 0
mumps
I maybe wrong, but that looks like a permanent loop.
 
Upvote 0
On top of what I said in post#3.
It wont let you select from the dropdown
 
Upvote 0
@ Fluff: Thanks for you input. When I tried it on the file the OP attached in his link, it worked properly.
 
Upvote 0
:oops: It would have helped if my DV was in col E not col H.
Apologies
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macros into the empty code window that opens up. Change the range in the code to suit your needs. Close the code window to return to your sheet.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If HasValidation(Range("E2:E5")) Then
        Exit Sub
    Else
        Application.Undo
        MsgBox "Error: You cannot paste data into these cells." & _
        "Please select from the drop-down list.", vbCritical
        Application.CutCopyMode = False
    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

hi mumps, thank for the code but it is not giving the desired results in my sheet.
what i want to do is, I want to copy paste the data from other sheets in column E. I want a code which allows pasting of values which are listed in drop down values only. If I paste non listed value, then it should run the macro and show error.
for example, in list I have "Customer", but if i copy "Customers" from other sheet, then it should not be accepted in paste. but if i copy customer, then it can allow paste.
multiple rows can be pasted at a time.
if u see my sheet, i already have a code in column C which allows to paste 15 character length only. if pasted value is <> 15 then it is deleted.
 
Upvote 0
@Fluff: No apologies necessary. Been there, done that!!! (Many times) :) @NonStopLeo: The values in your current drop down list do not have 15 characters. Are you saying that you want to accept any current value if selected from the drop down list, but if they are pasted from somewhere else, the length of each value must be exactly 15 or can it be 15 character's or less?
 
Upvote 0
@Fluff: No apologies necessary. Been there, done that!!! (Many times) :) @NonStopLeo: The values in your current drop down list do not have 15 characters. Are you saying that you want to accept any current value if selected from the drop down list, but if they are pasted from somewhere else, the length of each value must be exactly 15 or can it be 15 character's or less?

No no no...things are mixed up...
I have code in column c, which is working fine i.e whenever i copy from anywhere and paste in column C, it keeps the values which has 15 characters and delete all others.

Now come to column E, here I have a drop down list. I can select any value in this column from drop down list. also i can copy data from somewhere else and paste in this column E. currently if I paste in column E, it is also accepting values which r in the drop down list and even also accepting thos values which are not in the list., Now i need a code which which scan the column after pasting, and if there is any value pasted which doesnt belong to the list, that should be deleted and error message should appear.

hope I have explained it well.
 
Upvote 0
I think I understand now. Click here to download your file. You have 2 Worksheet_Change macros so they have to be combined into one because Excel allows only one of this type of macro per worksheet. I have added Sheet2 which contains the source data for the drop down list in column E of Sheet1. This was necessary because the macro refers to this list to compare whatever values you paste into column E.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top