Allow a user to stop an active VBA loop

  • [SIZE=11px][/SIZE]


    [SIZE=11px]
    Please help with a question:
    is there a clean way for a user to stop an executing VBA loop?

    I know about coding with a timer and about how to do it with Doevents.
    Neither are very good solutions. I also don’t like the “modify the registry” option.


    One thing that (almost) works well is to send a {BREAK}. A coded example is
    with this post. The Msgbox immediately pops up its message after the break
    key(s) are pressed.

    However:

    [SIZE=11px]



    • Different computer brands use different keyboard mappings for {BREAK}.
      The method to exit the loop needs to be uniform for everyone.

    • My Dell Inspirion 7570 laptop requires four keys to be held down
      simultaneously to get the {BREAK} to VBA. That’s impractical for a user.

    • The four keys are: Cntl+Fn+Shift+b. It seems there is no way to map the
      Fn key to application.onkey. Fn is not on the Microsoft page:
      [indent]
      https://docs.microsoft.com/en-…i/Excel.Application.OnKey
      [/indent]

    [/SIZE]



    [SIZE=11px]Is there another way to send a break to a VBA loop? If not, is there some other
    clean way for a user to stop an executing loop?

    Any help would be very much appreciated.


    Code
    1. Sub SimpleLoop() On Error GoTo handleCancel Application.EnableCancelKey = xlErrorHandler For Nbr = 1 To 20 Application.Wait (Now + TimeValue("0:00:01")) Next Exit Sub handleCancel: MsgBox "You Cancelled." End Sub

    [/size][/SIZE]