1

I know this issue may sound like something that has been solved a million times, however all of the fixes that I have found haven't helped.

I have a horizontal cell range of numbers which I want to be displayed in a data validation list. Easily enough I can just set the DV list as =A1:H1. My issue is that I would then like to remove any duplicate values without creating any new lists of data.

Any ideas?

3
  • You could use VBA to do that, but I'm not sure if it can be achieved without VBA Commented Feb 12, 2018 at 15:38
  • It would be great if this could be done without using VBA Commented Feb 12, 2018 at 15:45
  • I found this link that might be interesting, but honestly speaking, I didn't understand how it works. mrexcel.com/forum/excel-questions/… Commented Feb 12, 2018 at 15:56

1 Answer 1

2

Select the cells you want the DV to be applied and run this short macro:

Sub uniDV()
    Dim r As Range
        For Each r In Selection
            With Selection.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:=unik(Range("A1:H1"))
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With
        Next r
End Sub

Public Function unik(rng As Range) As String
    Dim c As Collection, r As Range
    Set c = New Collection
    On Error Resume Next
    For Each r In rng
        v = r.Value
            c.Add v, CStr(v)
        If Err.Number = 0 Then
            unik = unik & "," & v
        Else
            Err.Number = 0
        End If
    Next r
    On Error GoTo 0
    unik = Mid(unik, 2)
End Function

enter image description here

The function constructs a comma-separated string and the sub applies the DV.

Sign up to request clarification or add additional context in comments.

2 Comments

If an in-built function is not available then I will be looking for something like this however it is for multiple ranges of data. I would be looking for something that said "For each cell in range, create data validation list in [specified cell] consisting of unique data from [specified range].. Is this possible?
@JackCampion It is possible.............we wouuld need to know which ranges to apply the DV and which ranges contain the DV lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.