Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default How to cancel midway?

Dear Excel Gurus,

I have VBA code that takes about 5 minutes to run. Sometimes, I want to
cancel it halfway.

Can I add a new "Cancel" button to cancel the operation? What code should I
put for that button?

Thanks for your advice.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to cancel midway?

The simplest way is to set the Application.EnableCancelKey property to
xlErrorHandler. This will raise a run time error when the user presses CTRL
BREAK. For example,

Sub AAA()

Application.EnableCancelKey = xlErrorHandler
On Error GoTo ErrH:
'
' your code here
'
Exit Sub
ErrH:
If Err.Number = 18 Then
MsgBox "You pressed CTRL+BREAK"
Else
' other error handling
End If

End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Sing" wrote in message
...
Dear Excel Gurus,

I have VBA code that takes about 5 minutes to run. Sometimes, I want to
cancel it halfway.

Can I add a new "Cancel" button to cancel the operation? What code should
I
put for that button?

Thanks for your advice.



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
Workbook_BeforeClose(Cancel As Boolean) - Cancel won't work gpmichal Setting up and Configuration of Excel 1 May 12th 09 02:33 AM
Input box to cancel sub when Cancel is clicked. PCLIVE Excel Programming 5 September 5th 06 03:19 PM
Disabling 'Cancel' option when saving work (Yes/No/Cancel) [email protected] Excel Programming 0 July 11th 06 09:28 PM
Cancel button to cancel the whole macro excelnut1954 Excel Programming 3 January 17th 06 08:27 PM
Cancel Macro is user selects 'cancel' at save menu Mark Excel Programming 1 April 6th 05 05:45 PM


All times are GMT +1. The time now is 12:06 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"