Dictionary help.. Newbie

sovereign

Board Regular
Joined
Jul 10, 2012
Messages
102
So, i have been introduced to dictionary's so thought i would have a play...

What i would like to do is add 'ref' as a key and assign "Sheets(2).Cells(row.row, TotalClaimCol).Value" value to this key.

To avoid duplicates i have added if statement to check if exists

When i run this code i get an error saying the item already exists.

Any help would be appreciated, also can you assign multiple items too one key?

Cheers

Code:
For Each row In [kam_data[claimsent]].Rows


If CLng(row.Value) < startdate Then
Ref = Sheets(2).Cells(row.row, RefCol).Value
FoundMatch = False


    '       Has item been added yet?
    If dict.Exists(Ref) Then
        End If
    ' add item
    Else
      dict.Add Ref, Sheets(2).Cells(row.row, TotalClaimCol).Value
    End If
    ' total items
    Dim item As Variant
    For Each item In dict.Items
    TotalClaimAmnt = TotalClaimAmnt + item
    Next item
             

next
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You should be checking that it doesn't exist, you can't have multiple of the same key:

Rich (BB code):
    If Not dict.Exists(Ref) Then
        dict.Add Ref, Sheets(2).Cells(row.row, TotalClaimCol).Value
    End If

You can put anything in the value, so if you want multiple values then a collection, array or another dictionary would be logical choices
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top