this key is already associated with an element of this collection

M

muybn

A macro I'm creating (see partial code below) reads tab-delimited fields from
a text file. The strange thing is that after I shut it down then start it up
again for testing, I receive the message "This key is already associated with
an element of this collection"--almost as if the process is kept in memory
from the previous session. The hangup occurs on the line that reads
"arrHeaderNames(intCtr)" at the end.

Then, even more strange, when I bump the yellow highlighted step indicator
back up about three lines to "Line Input #1," it runs OK.

Thanks in advance for any thoughts.

'get some key values from source file and assign them to main variables
Open strPathWkFile For Input As #1
Line Input #1, strScrap
arrHeaderNames = Split(strScrap, Chr(9))
For intCtr = LBound(arrHeaderNames) To UBound(arrHeaderNames)
colHeader.Add Item:=Trim(str(intCtr)), Key:=arrHeaderNames(intCtr)
Next intCtr
intCtr = 0
Do While Not EOF(1)
intCtr = intCtr + 1
Line Input #1, strScrap
arrFieldData = Split(strScrap, Chr(9))
strLead_ID = arrFieldData(Val(colHeader.Item("Lead_ID")))

I even tried changing the value of intCtr in the troubled line from 0 to 1
but to no avail.
 
R

Russ

I am not sure if this is your problem, but once you open a file with the
method you a using, the inside the open file position pointer may be staying
where you left off in the file in memory until you properly close the file
with the 'Close' code or it gets a command to process more of the file,
thereby moving the position pointer somewhere else. (The 'Seek' command
directly moves this file position pointer.) So if you get interrupted by an
error in your macro you could try this: stop the macro, and go to the
immediate window and type 'close #1', without the single quotes, and hit
return.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top