Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default EnableCancelKey with On Error Resume Next

Hi,

I want to brute force errors in a long loop with
On Error.Resume Next

But I also want to give user a chance to Ctrl Break and
clean up in an orderly way.

Variations on the following seem to work intermittently
but unreliably (.EnableCancelKey = xlErrorHandler inside
the loop slightly more reliable):

Sub ErrorTest()

With Application
For i = 1 To 100000
On Error GoTo ErrH
.EnableCancelKey = xlErrorHandler
'sometimes catches Ctrl Break here

On Error Resume Next
'don't want to break here
.EnableCancelKey = xlDisabled
x = 1 / 0 ' allow this error
Next

ErrH:
.EnableCancelKey = xlInterrupt
End With

MsgBox i & " Err " & Err.Number '18 = user Ctrl Break
End Sub

Not sure if what I'm trying to achieve is possible but any
solution appreciated.

TIA,
Paul


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default EnableCancelKey with On Error Resume Next

Again, help comes to the rescue:

xlErrorHandler The interrupt is sent to the running procedure as an
error, trappable by an error handler set up with an On Error GoTo statement.
The trappable error code is 18.


So if err.Number = 18 then you can jump out or if it isn't, continue on

as an example:


Sub Tester10()
Application.EnableCancelKey = xlErrorHandler
On Error GoTo ErrHandler
For i = 1 To 100000
If Rnd() < 0.001 Then
Err.Raise 1000
End If
Cells(1, 1) = i
Next
Exit Sub
ErrHandler:
If Err.Number < 18 Then
Resume Next
End If
MsgBox "You cancelled"
End Sub

--
Regards,
Tom Ogilvy



"Paul S" wrote in message
...
Hi,

I want to brute force errors in a long loop with
On Error.Resume Next

But I also want to give user a chance to Ctrl Break and
clean up in an orderly way.

Variations on the following seem to work intermittently
but unreliably (.EnableCancelKey = xlErrorHandler inside
the loop slightly more reliable):

Sub ErrorTest()

With Application
For i = 1 To 100000
On Error GoTo ErrH
.EnableCancelKey = xlErrorHandler
'sometimes catches Ctrl Break here

On Error Resume Next
'don't want to break here
.EnableCancelKey = xlDisabled
x = 1 / 0 ' allow this error
Next

ErrH:
.EnableCancelKey = xlInterrupt
End With

MsgBox i & " Err " & Err.Number '18 = user Ctrl Break
End Sub

Not sure if what I'm trying to achieve is possible but any
solution appreciated.

TIA,
Paul




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default EnableCancelKey with On Error Resume Next

Tom,

Yes of course, what was I thinking of!

Many thanks for the "rescue".

Regards,
Paul
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
On error resume next not working cluckers Excel Discussion (Misc queries) 3 November 25th 09 08:12 PM
On error resume next statement ignored! J S Excel Programming 2 January 30th 04 10:58 PM
ON ERROR RESUME NEXT D.S.[_3_] Excel Programming 7 December 1st 03 09:40 AM
On Error Resume Next D.S.[_3_] Excel Programming 1 November 28th 03 04:52 PM
On Error Resume Next Mike[_58_] Excel Programming 3 November 23rd 03 05:09 PM


All times are GMT +1. The time now is 09:45 PM.

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"