problem capturing Run-Time error 18

L

Legacy 146544

Guest
Hi all,


Im am creating a powerpoint like presentation in excel. The presentation is invoked by button click and I need to capture the error variable when ESC is pressed. This practical use of it is: the excel document should run on a tv screen, showing each page about 30 seconds. I need the code to be cancelled when the user presses ESC.

For testing purposes I have added the for x = 1 to 2 in the code below. In fact it should run infinitely until cancelled by ESC. When I press ESC on this code; Excel rushes through entire rest of the loop (for-next, I have also tried with do-loop) instead of reaching the errorstatement.

This is my code:

Private Sub CommandButton2_Click()

On Error GoTo UserInterrupt
Application.EnableCancelKey = xlErrorHandler

Dim Uur As Variant
Dim Minuut As Variant
Dim Seconde As Variant
Dim WachtTot As Variant
Dim x As Integer, SecPerPag As Integer, TotaalTijd As Integer

SecPerPag = Blad22.Range("D4").Value

Application.DisplayFullScreen = True

For x = 1 To 2
Blad11.Activate
Uur = Hour(Now())
Minuut = Minute(Now())
Seconde = Second(Now()) + SecPerPag
WachtTot = TimeSerial(Uur, Minuut, Seconde)
Application.Wait WachtTot

Blad12.Activate
Uur = Hour(Now())
Minuut = Minute(Now())
Seconde = Second(Now()) + SecPerPag
WachtTot = TimeSerial(Uur, Minuut, Seconde)
Application.Wait WachtTot

Blad13.Activate
Uur = Hour(Now())
Minuut = Minute(Now())
Seconde = Second(Now()) + SecPerPag
WachtTot = TimeSerial(Uur, Minuut, Seconde)
Application.Wait WachtTot

Blad15.Activate
Uur = Hour(Now())
Minuut = Minute(Now())
Seconde = Second(Now()) + SecPerPag
WachtTot = TimeSerial(Uur, Minuut, Seconde)
Application.Wait WachtTot

Blad19.Activate
Uur = Hour(Now())
Minuut = Minute(Now())
Seconde = Second(Now()) + SecPerPag
WachtTot = TimeSerial(Uur, Minuut, Seconde)
Application.Wait WachtTot

Next x
Application.DisplayFullScreen = False
Exit Sub

UserInterrupt:
If Err.Number = 18 Then
Application.DisplayFullScreen = False
Exit Sub
End If

End Sub

Who knows why pressing ESC does not make the procedure go to the errorhandling statement?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Thx a lot for your response.

I've worked around the problem for now, but will definitely check your suggestion to improve my code!

Cheers
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top