Swolerosis
Swolerosis

Reputation: 93

Dictionary add if doesn't exist

I have a data set with duplicates and want to loop through the data and add if the key doesn't exist.

Dim SSBIds As New Scripting.Dictionary
Dim key As Variant
For i = 2 To endSSB
    For Each key In SSBIds
        If Not SSBIds.Exists(key) Then
            SSBIds.Add SSB.Cells(i, 1).Value2, i
        End If
    Next
Next i

endSSB is just the last row of the data set. I just keep staring at this and can't figure out why it won't work.

Upvotes: 2

Views: 7620

Answers (1)

user3598756
user3598756

Reputation: 29421

With:

Dim SSBIds As New Scripting.Dictionary

you are defining a brand new Dictionary, and then with:

For Each key In SSBIds

you are trying to loop through its keys, which however are empty...

Maybe you're after this

Dim SSBIds As New Scripting.Dictionary
Dim i As Long
With SSB
    For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
        If Not SSBIds.Exists(.Cells(i, 1).Value2) Then SSBIds.Add .Cells(i, 1).Value2, i
    Next
End With

Upvotes: 2

Related Questions