#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9
Default Run-time error '457'

I'm running Office 2003 in Windows XP. In Excel, I have a a set of macros
that run on a file to parse out the data into a format I can use for
creating mail-merge letters in Word. At one point in the process I have a
worksheet that looks similar to this:

DistrictName LetterID
Azure Hills C08-0300 Jan
Babylon C08-0301 Mar
Babylon C08-0301 Mar
Corners C08-0302 Mar
Corners C08-0302 May
Corners C08-0302 May

Essentially the first column is the DistrictName, the 2nd column is the
LetterID, the 3rd column is a month and the 4th column is where a final
value for a filename will be written.

I have a loop that grabs the values in A1 and B1 (DistrictName and
LetterId), assigns them to the variables DistrictName and LetterID and then
sends them to a Function entitled DynFilename. When the function returns a
value, that value is written into the 4th column and then the focus is moved
down one row and we loop. My problem occurs within the function DynFilename.

(If all of the above is extraneous information, I apologize. I just want to
make sure I provide enough information so you can understand what I am
trying to do.)

The function DynFilename looks like this:

Function DynFilename(DistrictName, LetterId) As String

Dim Rng As Range
Dim cUnique As Collection
Dim cell As Range
Dim sh As Worksheet
Dim vNum As Variant

Set sh = ActiveSheet
Set Rng = sh.Range("A2", sh.Range("A2").End(xlDown))
Set cUnique = New Collection

'Assign a value to DynFilename
DynFilename = DistrictName & "_" & LetterId & "_XX_"

'This loops through cells in the worksheet and checks to see if the value
'in the currently selected cell is equal to the value in the variable
'DistrictName AND whether the value in the cell directly to the right
'of the currently selected cell is equal to the value in the variable
'LetterId. If it is, then the value in the cell 2 to the right of the
'currently selected cell is placed into the collection colMonths
On Error Resume Next
For Each cell In Rng.Cells
If cell.Value = DistrictName And cell.Offset(0, 1).Value =
LetterId Then
cUnique.Add Format(cell.Offset(0, 2).Value, "mmm"),
Format(cell.Offset(0, 2).Value, "mmm")
On Error GoTo 0
End If
Next cell

'The value in DynFilename is appended with the values that
'are in colMonths
For Each vNum In cUnique
DynFilename = DynFilename & vNum
Next vNum

'Append "08" to the end of DynFilename
DynFilename = DynFilename & "08"

End Function

Essentially I'm running this function so I can read in all the months that
are associated with a given district and letterid and then uniquely identify
those months to append to the filename. So if there were 4 entries for a
district and they had months "Jan, Mar, Mar, Mar", then I only want to show
"JanMar" when I generate to final filename.

When I run this, I get the following error:
Run-time error '457'
This key is already associated with an element of this collection

In stepping through the code, it appears that everything works fine for the
first loop through. The error appears when trying to process the second row
of data.

Admittedly, I haven't used this particular code in months...but it was
working fine when I ran it back in January. How do I get this code working
again?

-Brian


  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9
Default Run-time error '457'

Apparently replacing "On Error GoTo 0" with "On Error Resume Next" got rid
of the error.

-Brian

"Brian Beck" wrote in message ...
I'm running Office 2003 in Windows XP. In Excel, I have a a set of macros
that run on a file to parse out the data into a format I can use for
creating mail-merge letters in Word. At one point in the process I have a
worksheet that looks similar to this:

DistrictName LetterID
Azure Hills C08-0300 Jan
Babylon C08-0301 Mar
Babylon C08-0301 Mar
Corners C08-0302 Mar
Corners C08-0302 May
Corners C08-0302 May

Essentially the first column is the DistrictName, the 2nd column is the
LetterID, the 3rd column is a month and the 4th column is where a final
value for a filename will be written.

I have a loop that grabs the values in A1 and B1 (DistrictName and
LetterId), assigns them to the variables DistrictName and LetterID and
then sends them to a Function entitled DynFilename. When the function
returns a value, that value is written into the 4th column and then the
focus is moved down one row and we loop. My problem occurs within the
function DynFilename.

(If all of the above is extraneous information, I apologize. I just want
to make sure I provide enough information so you can understand what I am
trying to do.)

The function DynFilename looks like this:

Function DynFilename(DistrictName, LetterId) As String

Dim Rng As Range
Dim cUnique As Collection
Dim cell As Range
Dim sh As Worksheet
Dim vNum As Variant

Set sh = ActiveSheet
Set Rng = sh.Range("A2", sh.Range("A2").End(xlDown))
Set cUnique = New Collection

'Assign a value to DynFilename
DynFilename = DistrictName & "_" & LetterId & "_XX_"

'This loops through cells in the worksheet and checks to see if the value
'in the currently selected cell is equal to the value in the variable
'DistrictName AND whether the value in the cell directly to the right
'of the currently selected cell is equal to the value in the variable
'LetterId. If it is, then the value in the cell 2 to the right of the
'currently selected cell is placed into the collection colMonths
On Error Resume Next
For Each cell In Rng.Cells
If cell.Value = DistrictName And cell.Offset(0, 1).Value =
LetterId Then
cUnique.Add Format(cell.Offset(0, 2).Value, "mmm"),
Format(cell.Offset(0, 2).Value, "mmm")
On Error GoTo 0
End If
Next cell

'The value in DynFilename is appended with the values that
'are in colMonths
For Each vNum In cUnique
DynFilename = DynFilename & vNum
Next vNum

'Append "08" to the end of DynFilename
DynFilename = DynFilename & "08"

End Function

Essentially I'm running this function so I can read in all the months that
are associated with a given district and letterid and then uniquely
identify those months to append to the filename. So if there were 4
entries for a district and they had months "Jan, Mar, Mar, Mar", then I
only want to show "JanMar" when I generate to final filename.

When I run this, I get the following error:
Run-time error '457'
This key is already associated with an element of this collection

In stepping through the code, it appears that everything works fine for
the first loop through. The error appears when trying to process the
second row of data.

Admittedly, I haven't used this particular code in months...but it was
working fine when I ran it back in January. How do I get this code working
again?

-Brian



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Run-time error '457'

I think you had the "on error resume next in the wrong spot:


For Each cell In Rng.Cells
If cell.Value = DistrictName _
And cell.Offset(0, 1).Value = LetterId Then
On Error Resume Next
cUnique.Add Format(cell.Offset(0, 2).Value, "mmm"), _
Format(cell.Offset(0, 2).Value, "mmm")
On Error GoTo 0
End If
Next cell

or you had the "on error goto 0 in the wrong spot:

On Error Resume Next
For Each cell In Rng.Cells
If cell.Value = DistrictName _
And cell.Offset(0, 1).Value = LetterId Then
cUnique.Add Format(cell.Offset(0, 2).Value, "mmm"), _
Format(cell.Offset(0, 2).Value, "mmm")
End If
Next cell
On Error goto 0

In either case, you want to ignore any error caused by trying to add a duplicate
item to the collection.



Brian Beck wrote:

Apparently replacing "On Error GoTo 0" with "On Error Resume Next" got rid
of the error.

-Brian

"Brian Beck" wrote in message ...
I'm running Office 2003 in Windows XP. In Excel, I have a a set of macros
that run on a file to parse out the data into a format I can use for
creating mail-merge letters in Word. At one point in the process I have a
worksheet that looks similar to this:

DistrictName LetterID
Azure Hills C08-0300 Jan
Babylon C08-0301 Mar
Babylon C08-0301 Mar
Corners C08-0302 Mar
Corners C08-0302 May
Corners C08-0302 May

Essentially the first column is the DistrictName, the 2nd column is the
LetterID, the 3rd column is a month and the 4th column is where a final
value for a filename will be written.

I have a loop that grabs the values in A1 and B1 (DistrictName and
LetterId), assigns them to the variables DistrictName and LetterID and
then sends them to a Function entitled DynFilename. When the function
returns a value, that value is written into the 4th column and then the
focus is moved down one row and we loop. My problem occurs within the
function DynFilename.

(If all of the above is extraneous information, I apologize. I just want
to make sure I provide enough information so you can understand what I am
trying to do.)

The function DynFilename looks like this:

Function DynFilename(DistrictName, LetterId) As String

Dim Rng As Range
Dim cUnique As Collection
Dim cell As Range
Dim sh As Worksheet
Dim vNum As Variant

Set sh = ActiveSheet
Set Rng = sh.Range("A2", sh.Range("A2").End(xlDown))
Set cUnique = New Collection

'Assign a value to DynFilename
DynFilename = DistrictName & "_" & LetterId & "_XX_"

'This loops through cells in the worksheet and checks to see if the value
'in the currently selected cell is equal to the value in the variable
'DistrictName AND whether the value in the cell directly to the right
'of the currently selected cell is equal to the value in the variable
'LetterId. If it is, then the value in the cell 2 to the right of the
'currently selected cell is placed into the collection colMonths
On Error Resume Next
For Each cell In Rng.Cells
If cell.Value = DistrictName And cell.Offset(0, 1).Value =
LetterId Then
cUnique.Add Format(cell.Offset(0, 2).Value, "mmm"),
Format(cell.Offset(0, 2).Value, "mmm")
On Error GoTo 0
End If
Next cell

'The value in DynFilename is appended with the values that
'are in colMonths
For Each vNum In cUnique
DynFilename = DynFilename & vNum
Next vNum

'Append "08" to the end of DynFilename
DynFilename = DynFilename & "08"

End Function

Essentially I'm running this function so I can read in all the months that
are associated with a given district and letterid and then uniquely
identify those months to append to the filename. So if there were 4
entries for a district and they had months "Jan, Mar, Mar, Mar", then I
only want to show "JanMar" when I generate to final filename.

When I run this, I get the following error:
Run-time error '457'
This key is already associated with an element of this collection

In stepping through the code, it appears that everything works fine for
the first loop through. The error appears when trying to process the
second row of data.

Admittedly, I haven't used this particular code in months...but it was
working fine when I ran it back in January. How do I get this code working
again?

-Brian


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Excel Worksheet Functions 1 May 3rd 08 02:35 PM
Receiving run-time error only part of the time [email protected] Excel Discussion (Misc queries) 0 August 11th 07 12:01 AM
run-time error '91'-Close Button error ASCO IS Help Excel Discussion (Misc queries) 1 May 8th 06 04:25 PM
Run time error 1004, General ODBC error [email protected] New Users to Excel 0 September 19th 05 01:41 AM


All times are GMT +1. The time now is 10:23 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"