Scripting Dictionnary - key is already associated with an element of this collection
Hi,
I am new to scripting dictionnary.
I want to create two seperate dictionnary that will hold different information.
Here is my code :
Code:
sub test()
col_family = ws1.Range("a1:cz3").Find("family", , LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
col_legal_entity = ws1.Range("a1:cz3").Find("legal entity", , LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
col_broker = ws1.Range("a1:cz3").Find("broker", , LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
col_kyc = ws1.Range("a1:cz3").Find("KYC Responsibility", , LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Column
kyc = ThisWorkbook.Sheets("KYC Responsibility").Range("A1").CurrentRegion.Value
Set mergeR = ActiveSheet.Range("A1").CurrentRegion
merge = mergeR.Value
Set dic = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(kyc)
dic.Add kyc(i, 1) & ":" & kyc(i, 2), kyc(i, 3)
Next i
For i = 2 To UBound(merge)
s = merge(i, col_family) & ":" & merge(i, col_broker)
If dic.exists(s) Then
merge(i, col_legal_entity) = dic.Item(s)
Else
merge(i, col_legal_entity) = "N/A"
End If
Next i
mergeR.Value = merge
'******
kyc = ThisWorkbook.Sheets("KYC Responsibility").Range("A1").CurrentRegion.Value
Set mergeR = ws1.Range("A1").CurrentRegion
merge = mergeR.Value
Set dic = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(kyc)
dic.Add kyc(i, 2) & ":" & kyc(i, 3), kyc(i, 4)
Next i
For i = 2 To UBound(merge)
s = merge(i, col_broker) & ":" & merge(i, col_legal_entity)
If dic.exists(s) Then
merge(i, col_kyc) = dic.Item(s)
Else
merge(i, col_kyc) = "N/A"
End If
Next i
mergeR.Value = merge
The second part (after '*****) triggers an error at dic.add
Is there a line I can insert before that that would clear all the information of the existing dictionnary?
What is the proper way to do this?
Thank you.