PDA

View Full Version : Interrupting a macro



shankar
04-14-2009, 11:27 PM
Hi, if I have a message box within a loop like the program below-

Sub test()
Dim i As Integer
For i = 1 To 10
MsgBox i
Next
End Sub

I am not able to interrupt it using Ctrl Break. I have to hit the OK button 10 times. Surely there must be a way of stopping the execution after the first msgbox, right? I tried other keys like just break, or esc, but nothing seems to halt the execution of the macro. Is it possible to do it? Thanks.

shankar

GTO
04-15-2009, 12:05 AM
I just tried it several times, broke fine. I seem to recall that I had some bizarre similar thing happen at home on my laptop, and that scroll lock or num lock or some other wacky key needed turned off or on...

Mark

JONvdHeyden
04-15-2009, 12:50 AM
I've not come across this before either but I wonder if EnableCancelKey is stuck on xlDisabled. Try this:


Sub test()
Dim i As Integer
Application.EnableCancelKey = xlInterrupt
For i = 1 To 10
MsgBox i
Next
End Sub

shankar
04-15-2009, 01:35 AM
Oh, I found the problem.. on my Dell laptop, Delete and Break are shared by the same key, Break needs the Fn key to be pressed simultaneously. So ctrl+brk is really Fn+Ctrl+Break (3 keys to be pressed simultaneously).

Thanks anyway. On a different note, I have two minor issues with the VBA editor for Excel -
1. Auto-indent doesn't work even when I have it on under Editor options with tab width 4. Every new line starts from column 1. Coming from Visual C++, I find this a big irritant.

2. Is there any brackets/block begin/end matching in this editor like ctrl+] in Visual C++?

I am using Office 2007. Thanks.

Shankar

Jan Karel Pieterse
04-15-2009, 03:51 AM
All the autoindent does is indent to the same tabstop as the previous line, further indenting has to be done manually.
There is an excellent addin by stephen Bullen called Smart Indenter:
www.oaltd.co.uk (http://www.oaltd.co.uk)