I was wondering if there was a way in VBA to create a data validation dropdown list based on the items in a collection. Basically, what I have done is searched a database for a column header, selected items in that column, filtered out the unique values, and added those values to a collection. Now I would like those values added as a dropdown list through data validation. See code below:
If you know of a way to do this, it would be greatly appreciated.
Joe
Code:
Sub ColH()
Dim c As Range, i As Integer, e As Range, cells1 As Collection, cell1 As Range, range1 As Range
' Search for specific column header
For Each c In Range("A1:AA1")
If InStr(1, c, "Program Manager", vbTextCompare) = 1 Then
d = c.Address
End If
Next c
'Select the cells I need and filter out unique values
Set e = Range(Range(d).Offset(1, 0), Range(d).End(xlDown).Offset(-13, 0))
With e
.AdvancedFilter xlFilterCopy, , Range("BH2"), True
End With
Set range1 = Range("BH2", Range("BH" & Rows.Count).End(xlUp))
'Sort them and add to collection
Range(Range("BH2").Offset(1, 0), Range("BH2").End(xlDown).Offset(-1, 0)).Sort Range("BH3"), xlAscending
Set cells1 = New Collection
For Each cell1 In range1.Cells
Key = cell1.Row - range1.Row & "," & cell1.Column - range1.Column
cells1.Add cell1, Key
Next cell1
'Clear the range where they were sorted
With range1
.ClearContents
.Borders.LineStyle = xlNone
End With
Debug.Print cells1.Count
End Sub
If you know of a way to do this, it would be greatly appreciated.
Joe