Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Can't trap error when checking for keys not in Collection

Hello,

For some reason when i try to remove a key from a Collection where that key
hasn't been added yet, i cannot trap the error when it occurs within an error
handler. All i get is a error dialog box that pops up that says "Invalid
procedure call or argument"

the code snippet is as follows:

On Error GoTo ERR_HANDLER
myCollection.Remove ("key1")
..
..
..
ERR_HANDLER:
..code here does not get executed when "key1" does not exist in myCollection.

How i can trap this error and continue processing?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 903
Default Can't trap error when checking for keys not in Collection

I presume you included on error resume goto
for your error handler.


msgbox err.code & " -- " & err.description

to identify the actual error, you can then code off of the err.code
to do what you want.
--
---
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

"BW" wrote in message ...
Hello,

For some reason when i try to remove a key from a Collection where that key
hasn't been added yet, i cannot trap the error when it occurs within an error
handler. All i get is a error dialog box that pops up that says "Invalid
procedure call or argument"

the code snippet is as follows:

On Error GoTo ERR_HANDLER
myCollection.Remove ("key1")
.
.
.
ERR_HANDLER:
.code here does not get executed when "key1" does not exist in myCollection.

How i can trap this error and continue processing?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Can't trap error when checking for keys not in Collection

Why would you not want to check for the instance of the key before
removing.....thus no error would happen? Code to avoid errors (defensive
coding) can prove to be more efficient too.

On Error GoTo ERR_HANDLER
If Not myCollection("key1") Is Nothing
myCollection.Remove
EndIf


"BW" wrote:

Hello,

For some reason when i try to remove a key from a Collection where that key
hasn't been added yet, i cannot trap the error when it occurs within an error
handler. All i get is a error dialog box that pops up that says "Invalid
procedure call or argument"

the code snippet is as follows:

On Error GoTo ERR_HANDLER
myCollection.Remove ("key1")
.
.
.
ERR_HANDLER:
.code here does not get executed when "key1" does not exist in myCollection.

How i can trap this error and continue processing?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 903
Default Can't trap error when checking for keys not in Collection

I meant err.number instead of err.code
but mine was the wrong answer anyway see psmocko's reply.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Can't trap error when checking for keys not in Collection

I cannot recreate your problem in XL2K. Others appear to so perhaps it's
been introduced in later versions. Following worked as expected for me.

Sub test()
Dim col As New Collection
For i = 1 To 3
col.Add i * 10, Chr(i + 64)
Next

On Error GoTo errH
s = "B"
col.Remove s
s = "G"
col.Remove s ' goes to errh

Set col = Nothing
Exit Sub
errH:
Debug.Print s, Err.Number; Err.Description
' G 5 Invalid procedure call or argument
Resume Next
End Sub

Regards,
Peter T

"BW" wrote in message
...
Hello,

For some reason when i try to remove a key from a Collection where that

key
hasn't been added yet, i cannot trap the error when it occurs within an

error
handler. All i get is a error dialog box that pops up that says "Invalid
procedure call or argument"

the code snippet is as follows:

On Error GoTo ERR_HANDLER
myCollection.Remove ("key1")
.
.
.
ERR_HANDLER:
.code here does not get executed when "key1" does not exist in

myCollection.

How i can trap this error and continue processing?





  #6   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Can't trap error when checking for keys not in Collection

Strange,

I copied your code and single stepped in VBA for excel 2003 SP1.

Same results as before.... code does NOT jump to errH when col.Remove s,
where s = "G".


"Peter T" wrote:

I cannot recreate your problem in XL2K. Others appear to so perhaps it's
been introduced in later versions. Following worked as expected for me.

Sub test()
Dim col As New Collection
For i = 1 To 3
col.Add i * 10, Chr(i + 64)
Next

On Error GoTo errH
s = "B"
col.Remove s
s = "G"
col.Remove s ' goes to errh

Set col = Nothing
Exit Sub
errH:
Debug.Print s, Err.Number; Err.Description
' G 5 Invalid procedure call or argument
Resume Next
End Sub

Regards,
Peter T

"BW" wrote in message
...
Hello,

For some reason when i try to remove a key from a Collection where that

key
hasn't been added yet, i cannot trap the error when it occurs within an

error
handler. All i get is a error dialog box that pops up that says "Invalid
procedure call or argument"

the code snippet is as follows:

On Error GoTo ERR_HANDLER
myCollection.Remove ("key1")
.
.
.
ERR_HANDLER:
.code here does not get executed when "key1" does not exist in

myCollection.

How i can trap this error and continue processing?




  #7   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Can't trap error when checking for keys not in Collection

Hi again

Problem solved. It appears that in the Tools-Options-General tab,
selecint Error Trapping to "break on all errors" causes this behaviour. I
selected "break on unhandled errors" and now the the code jumps to the ErrH

"Peter T" wrote:

I cannot recreate your problem in XL2K. Others appear to so perhaps it's
been introduced in later versions. Following worked as expected for me.

Sub test()
Dim col As New Collection
For i = 1 To 3
col.Add i * 10, Chr(i + 64)
Next

On Error GoTo errH
s = "B"
col.Remove s
s = "G"
col.Remove s ' goes to errh

Set col = Nothing
Exit Sub
errH:
Debug.Print s, Err.Number; Err.Description
' G 5 Invalid procedure call or argument
Resume Next
End Sub

Regards,
Peter T

"BW" wrote in message
...
Hello,

For some reason when i try to remove a key from a Collection where that

key
hasn't been added yet, i cannot trap the error when it occurs within an

error
handler. All i get is a error dialog box that pops up that says "Invalid
procedure call or argument"

the code snippet is as follows:

On Error GoTo ERR_HANDLER
myCollection.Remove ("key1")
.
.
.
ERR_HANDLER:
.code here does not get executed when "key1" does not exist in

myCollection.

How i can trap this error and continue processing?




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
Checking Options in UserForms Collection monir Excel Programming 17 May 14th 05 04:40 PM
Trap whether it was TAB or arrow keys etc HotRod Excel Programming 3 April 29th 05 07:23 PM
Error Trap TEB2 Excel Programming 2 March 15th 05 05:34 PM
Collection Object Keys Todd Huttenstine Excel Programming 10 October 29th 04 01:14 PM


All times are GMT +1. The time now is 11:57 PM.

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

About Us

"It's about Microsoft Excel"