View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Zone Zone is offline
external usenet poster
 
Posts: 269
Default Spacebar Exits Macro

Dave, Thank you. Help goes to some length to explain that there are
implications to using EnableCancelKey, so apparently it should only be
used under controlled conditions to keep the Esc and Ctrl-Break keys
functional. I thought one way to do this was to use in a function. I
don't know the effect on the error trapping status of the calling
routine, though. See any problem with this code?
Seasons Greetings, James

Sub TryThis()
Dim j As Long
On Error Goto Problem
For j = 1 To 10000000
If LookForEsc() = True Then
MsgBox "You pressed Esc"
Exit Sub
End If
Next j
Problem:
End Sub

Function LookForEsc() As Boolean
LookForEsc = False
On Error GoTo handleCancel
Application.EnableCancelKey = xlErrorHandler
handleCancel:
If Err = 18 Then
LookForEsc = True
End If
End Function
Dave Peterson wrote:
How about a different key -- the ESC key???

If yes, take a look at Application.EnableCancelKey in VBA's help.

Zone wrote:

I have a long looping macro I'd like the user to be able to exit by pressing
the Spacebar. I'd like to accomplish this without the big overhead usually
associated with API calls. Any simple (short) way to do this? Probably a
wierd question! But thanks and happy holidays! James


--

Dave Peterson