简体   繁体   中英

Error - An Item with the same key has already been added

Upon opening a macro enabled Excel file, I get the following:

Run-Time Error '-2147024809 (80070057)': An Item with the same key has already been added.

I've seen lots of similar questions for other languages, but nothing for VBA. I did find one thing online for VBA, but it says it has something to do with the Scripting.Dictionary library.

However, I have no reference to MS Scripting Runtime, nor do I ever bind to it, and I have no Dictionaries or Arrays or anything of the sort in this file. The only thing I can think of is I have a small Userform that is only a Progress bar. Every item on that Userform is uniquely named, and none of them are words that should be reserved or used by the system.

Has anyone run into this or have any ideas?

Edit:

Ok, strange thing just happened...I closed the 2 other VBA Projects (Add-Ins) that were opened in the VBE, before opening my file, and the error didn't come up. Those 2 other files have been open the whole time I've been working on this file...

Then I closed Excel altogether...left the other 2 files open, and opened my file with no error. I have no idea...

Update:

Just got the same error opening a different file. In case my question wasn't already clear enough...Does anyone know what this error might have to with, or how to stop it?

This looks like a Scripting.Dictionary error but in facts the following code throws an error of code 457 which is different from yours and the error message for dictionaries is different too 'This key is already associated with an element of this collection' This can be verified with the code

Option Explicit

Sub Tst()
    On Error Resume Next
    Dim dic As New Scripting.Dictionary
    dic.Add 1, 0
    dic.Add 1, 0
    Debug.Print Err.Number, Err.Description
End Sub

So it is not your VBA code IMHO, but it looks rather like a C# error and we can verify this with some code, opening a Windows Forms C# project in Visual Studio and then edit Form1_Load to the following

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Dictionary<long, long> dicTest = new Dictionary<long, long>();
                dicTest.Add(1, 0);
                dicTest.Add(1, 0);
            }
            catch (Exception ex) {
                System.Windows.Forms.MessageBox.Show(string.Format("{0} {1}", ex.HResult, ex.Message));
            }
        }
    }
}

and this throws the message box with the error number and message that you reported. Thus it is a C# Dictionary error IMHO, and note how we trapped it with a try catch block whereas I think you have code C# that is not catching the error.

So next question is which workbook is responsible. I would suggest in your experiments using Application.EnableEvents = False which turns off event handling, such that event handlers written in any language VBA, C# , VB,NET do not run. Then you can run your workbook's initialisation routines manually, if no error occurs your workbook is not guilty whereas the other workbooks/addins must be guilty at which you report the bug to their author.

I hope you make progress with this. It is difficult to take this further without feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM