Thursday, September 12, 2013

Create Validation by Removing Duplicates

A
B
C
D
B
C
A
F
G
D
A

Option Explicit
Dim rowcount As Long
Dim myarray As New Collection, tempval As String, finaltempval As String

Sub addValidation()
On Error Resume Next
rowcount = Sheets(2).Range("a3").End(xlDown).Row
For i = 3 To rowcount
tempval = vbNullString
myarray.Add Range("A" & i), Range("A" & i)

Next i
For j = 1 To myarray.Count
    tempval = tempval & myarray(j) & ","

Next j

finaltempval = Mid(tempval, 1, Len(tempval) - 1)

With Range("B1").Validation

    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=finaltempval

End With
finaltempval = vbNullString
End Sub

No comments:

Post a Comment