The key for a Collection
item needs to be a string. So change this line:
oColl.Add vData(n, 1), vData(n, 1)
to this:
oColl.Add vData(n, 1), CStr(vData(n, 1))
Also, although you need the On Error Resume Next
so the code will skip over any attempts to add duplicates to the collection, you should only use it for that one line. Otherwise you risk masking other errors in your code. (The reason your code didn't have a runtime error was because the On Error Resume Next
, in addition to doing it's job of bypassing duplicates, was also skipping over any Adds
with numeric Keys
.
For that reason, I moved the line to just before the oColl.Add
and added On Error Goto 0
just after:
Here's the full routine:
Sub GetUniqueItems()
Dim vData As Variant, n&, lLastRow&, sMsg$
Dim oColl As Collection
lLastRow = Worksheets(Worksheets("Summary").Range("A1").Value).Cells(Rows.Count, "H").End(xlUp).Row
If lLastRow = 1 Then Exit Sub
vData = Worksheets(Worksheets("Summary").Range("A1").Value).Range("H2:H" & lLastRow)
Set oColl = New Collection
For n = LBound(vData) To UBound(vData)
On Error Resume Next
oColl.Add vData(n, 1), CStr(vData(n, 1))
On Error GoTo 0
Next n
For n = 1 To oColl.Count
sMsg = oColl(n)
Sheets("Summary").Cells(n + 3, 1).Value = Mid$(sMsg, 1)
Next n
End Sub
One last thing: you want to avoid statements like Dim oColl As New Collection
, and instead declare and set it in two steps as I did. For the reason see the Chip Pearson page and scroll down to "Don't Use Auto-Instancing Object Variables."