Here is some quick code based on that link... Toss it into a command button
and give it a try. It resets the application settings you may have changed,
and also give you the option to resume the loop...
Private Sub CommandButton1_Click()
Dim lng As Long
On Error GoTo ErrorHandler
Application.EnableCancelKey = xlErrorHandler
For lng = 1 To 1000000
Next lng
Exit Sub
ErrorHandler:
If Err = 18 Then
If MsgBox("Do you want to stop?", vbYesNo, "Quit?") = vbYes Then
Application.StatusBar = False
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End
Else
Resume
End If
End If
End Sub
Code:
--------------------
Sub CommandButton1()
Dim lng As Long
On Error GoTo ErrorHandler
Application.EnableCancelKey = xlErrorHandler
MsgBox "This may take a long time: press ESC to cancel"
For lng = 1 To 1000000
' do something here
Next lng
Exit Sub
ErrorHandler:
If Err = 18 Then
If MsgBox("Do you want to stop?", vbYesNo, "Quit?") = vbYes Then
Application.StatusBar = True 'false
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
MsgBox "macro stopped"
End
Else
Resume
End If
End If
--------------------
This is great and it works!
Again Thanks
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.