Runtime Error '457' help

Elliottj2121

Board Regular
Joined
Apr 15, 2021
Messages
50
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

I do not know what happened to my code or what I did differently. My code worked perfectly yesterday and something changed and I was hoping someone here could help. I am getting a "This key is already associated with an element of this collection" message. I have the section of code below and example data. Any help would be greatly appreciated!!

VBA Code:
Sub MasterARdue45O()
  Dim wbARDue45 As Workbook, wbWorkingARDue45 As Workbook
  Set wbARDue45 = OpenWkbARDue45
  Set wbWorkingARDue45 = OpenWkbWorkingARDue45
  Ardue45formatting wbARDue45
  MergeData wbARDue45, wbWorkingARDue45
  WorkingArdue45formatting wbWorkingARDue45
End Sub

Function OpenWkbWorkingARDue45() As Workbook
  Dim sPath As String, sName As String
  sName = "Working45.xlsx"
  sPath = Environ("USERPROFILE") & "\Desktop\" & sName
  Set OpenWkbWorkingARDue45 = Workbooks.Open(Filename:=sPath)
End Function
Function OpenWkbARDue45() As Workbook
  Dim sPath As String, sName As String
  sName = "ARdue45.xlsx"
  sPath = Environ("USERPROFILE") & "\Desktop\" & sName
  Set OpenWkbARDue45 = Workbooks.Open(Filename:=sPath)
End Function

Sub MergeData(wkbFrom As Workbook, wkbTo As Workbook)
  Dim wsFrom As Worksheet, wsTo As Worksheet, CrntIDs As Scripting.Dictionary
  Dim lFromRow As Variant, lToRow As Variant, r As Long, i As Long
  Set wsFrom = wkbFrom.Worksheets(1)
  Set wsTo = wkbTo.Worksheets(1)
  lToRow = LastRow(wsTo)
  If lToRow > 0 Then
    Set CrntIDs = New Dictionary
    For r = 2 To lToRow
      CrntIDs.Add CStr(wsTo.Cells(r, 3).Value), r
    Next r
  End If
  lFromRow = LastRow(wsFrom)
  If lFromRow > 0 Then
    For r = 2 To lFromRow
      If CrntIDs.Exists(CStr(wsFrom.Cells(r, 3).Value)) Then
        i = CrntIDs(CStr(wsFrom.Cells(r, 3).Value))
        wsFrom.Range("A" & r & ":D" & r).Copy wsTo.Cells(i, 1)
        wsFrom.Range("F" & r & ":I" & r).Copy wsTo.Cells(i, 6)
      Else
        wsFrom.Range("A" & r & ":I" & r).Copy wsTo.Cells(lToRow + 1, 1)
        lToRow = lToRow + 1
      End If
    Next r
  End If
End Sub
Function LastRow(sh As Worksheet) As Variant
  On Error Resume Next
  LastRow = sh.Cells.Find(what:="*", _
                          After:=sh.Range("A1"), _
                          lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
  On Error GoTo 0
End Function


This is the wsFrom worksheet
StatusOut Salesman #Customer NumberCOMPANY COMMENTS45 DaysDue 60BALANCECredit Manager
01820110756APPPLE A585360.11287986.532855728.70Ed
01820111984APPPLE A133513.060.00259423.40Ed
01820112105BANANA A4207.800.0028635.12Ed
04300400616CARROTA248383.781198045.852221126.87Mike
02250203839DOGA94276.000.00343407.39Katie
04810400213ELEPHANTI18762.82-353.5764820.39Hope
04810400570FLAMINGOA152.270.0015017.72Hope
04300400470GATORA1830853.4372479.547102673.30Katie
01350110221HIPPOA3435.680.00140687.56Ed
01350111320JERRYA9622.110.0051350.22Ed
02200202194KITEA263.94-161.73582.44Katie
01500110901LLAMAA162459.390.00556032.63Ed


This is the wbTo worksheet:
Status SalesNumberNAMENotes45 DaysDue 60BALANCECredit
01820110756APPPLE ACH 5-19674986.0454149428.80942801244.605Ed
01820111984APPPLE ACH 5-19133513.06060259423.3995Ed
Call01820112105BANANA EM 5-151039.68541026581.20974Ed
04300400616CARROTACH 5-19248383.78481198045.8512158360.728Mike
Call02250203839DOGEM 5-1543410.012750343407.3884Katie
Call04810400213ELEPHANTEM 5-1518762.82481-353.573096464820.38747Hope
Call04810400570FLAMINGOA152.2721972013859.149Hope
04300400470GATORACH 5-221845814.73657518.232596979415.053Katie
Fax01350110221HIPPOFax 5-163435.6847190140687.5646Ed
Fax01450101175JERRYFax 5-16116474.900616994.75792346526.7216Ed
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You must have duplicated values in column C.
 
Upvote 0
Well the message "This key is already associated with an element of this collection" implies an issue with your dictionary CrntIDs.

Specifically, that you are trying to add a key/value pair to that dictionary, where the key already exists (dictionary keys are indexed and must be unique)

So, I would debug the code and check what is being added to the dictionary at this line :

VBA Code:
For r = 2 To lToRow
    CrntIDs.Add CStr(wsTo.Cells(r, 3).Value), r
Next r

...while watching the existing keys in a watch window and confirming that each key does not already exist before you add it?

May be no harm to test for the existence of a key before adding the key/value pair as VBA won't check this for you automatically :

VBA Code:
For r = 2 To lToRow
    If Not CrntIDs.Exists(CStr(wsTo.Cells(r, 3).Value)) Then
        CrntIDs.Add CStr(wsTo.Cells(r, 3).Value), r
    End If
Next r


Unless of course you're expecting the same key to appear multiple times, in which case you need to reevaluate what you're using as a key for the dictionary in the first place...
 
Upvote 0
Well the message "This key is already associated with an element of this collection" implies an issue with your dictionary CrntIDs.

Specifically, that you are trying to add a key/value pair to that dictionary, where the key already exists (dictionary keys are indexed and must be unique)

So, I would debug the code and check what is being added to the dictionary at this line :

VBA Code:
For r = 2 To lToRow
    CrntIDs.Add CStr(wsTo.Cells(r, 3).Value), r
Next r

...while watching the existing keys in a watch window and confirming that each key does not already exist before you add it?

May be no harm to test for the existence of a key before adding the key/value pair as VBA won't check this for you automatically :

VBA Code:
For r = 2 To lToRow
    If Not CrntIDs.Exists(CStr(wsTo.Cells(r, 3).Value)) Then
        CrntIDs.Add CStr(wsTo.Cells(r, 3).Value), r
    End If
Next r


Unless of course you're expecting the same key to appear multiple times, in which case you need to reevaluate what you're using as a key for the dictionary in the first place...
How to watch the existing keys in a watch window at that line?
 
Upvote 0
How to watch the existing keys in a watch window at that line?

  1. Add a breakpoint at that line (right-click > Toggle > Breakpoint, or just click in the narrow margin to the left of the code window)
  2. Right-click on CrntIDs in the code and Add Watch...
  3. When the Add Watch dialog appears, modify the expression from CrntIDs to CrntIDs.Keys (or add a separate watch so you can see both the keys and values at the same time)
  4. Use F8 to step through the code line by line and monitor the dictionaries as you go
 
Upvote 0
  1. Add a breakpoint at that line (right-click > Toggle > Breakpoint, or just click in the narrow margin to the left of the code window)
  2. Right-click on CrntIDs in the code and Add Watch...
  3. When the Add Watch dialog appears, modify the expression from CrntIDs to CrntIDs.Keys (or add a separate watch so you can see both the keys and values at the same time)
  4. Use F8 to step through the code line by line and monitor the dictionaries as you go
Thank you for this! Sorry I am late on my response. What happened is in the column with the customer numbers there was a space in a cell a few rows below the data.
 
Upvote 0

Forum statistics

Threads
1,215,368
Messages
6,124,523
Members
449,169
Latest member
mm424

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