Two sets of data validation on the same cells?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
921
Office Version
  1. 365
  2. 2019
Is this possible?

I have cells D2 to D17 set with data validation to only allow values between 1 and 16. I also want to prevent duplicate entries. Is there a way to do this?

Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is this possible?

I have cells D2 to D17 set with data validation to only allow values between 1 and 16. I also want to prevent duplicate entries. Is there a way to do this?

Thanks
Try this...

Select the range D2:D17
Data Validation
Allow: Custom
Formula:

=AND(MOD(D2,1)=0,D2>=1,D2<=16,COUNTIF(D$2:D$17,D2)<2)

OK out
 
Upvote 0
Try this...

Select the range D2:D17
Data Validation
Allow: Custom
Formula:

=AND(MOD(D2,1)=0,D2>=1,D2<=16,COUNTIF(D$2:D$17,D2)<2)

OK out

I am getting an error when I try to apply that. I think it might not be in agreement with some VB (that maybe you gave me?) to convert "1" into "1st" etc:


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Formula
Case 1: Target.Value = Target.Value & "st"
Case 2: Target.Value = Target.Value & "nd"
Case 3: Target.Value = Target.Value & "rd"
Case 4 To 16: Target.Value = Target.Value & "th"
End Select
Application.EnableEvents = True

End Sub
 
Upvote 0
I am getting an error when I try to apply that. I think it might not be in agreement with some VB (that maybe you gave me?) to convert "1" into "1st" etc:


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Formula
Case 1: Target.Value = Target.Value & "st"
Case 2: Target.Value = Target.Value & "nd"
Case 3: Target.Value = Target.Value & "rd"
Case 4 To 16: Target.Value = Target.Value & "th"
End Select
Application.EnableEvents = True

End Sub
If you have that event macro then you'll want to tweak it to include the validation.

I'm not much of a programmer so someone else will need to help you with that.
 
Upvote 0
If you have that event macro then you'll want to tweak it to include the validation.

I'm not much of a programmer so someone else will need to help you with that.

Or could I modify the data validation to account for 1st through 16th as well?
 
Upvote 0
Or could I modify the data validation to account for 1st through 16th as well?
The menu command data validation won't work if the range has an event macro entering the data.

The menu command data validation will only work for manually entered data.
 
Upvote 0
Maybe this can help

I'm just trying to insert the validation rules provided by T. Valko in the Worksheet_Change event

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Target, Range("D2:D17")) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        If Target = "" Then Exit Sub
        
        Application.EnableEvents = False
        
        If Not IsNumeric(Target) Then GoTo HdlError
        Target = Int(Target)
        If Not ((Target >= 1) And (Target <= 16)) Then GoTo HdlError
        
        Select Case Target.Formula
            Case 1: Target.Value = Target.Value & "st"
            Case 2: Target.Value = Target.Value & "nd"
            Case 3: Target.Value = Target.Value & "rd"
            Case 4 To 16: Target.Value = Target.Value & "th"
        End Select
        
        If Application.CountIf(Range("D2:D17"), Target.Value) > 1 Then GoTo HdlError
        
        Application.EnableEvents = True
    End If
    
    Exit Sub
    
HdlError:
    MsgBox "Invalid or duplicate entry"
    Target = ""
    Target.Select
    Application.EnableEvents = True
End Sub

Hope this works

M.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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