0

This can be a very valuable if there is an answer to it:

You have a messagebox within a loop, and you can't break into the code because the Messagbox keeps popping up. Once the Messagebox is up, you can't really go back to the vba developement window to stop the program, because it is a modal window that takes over then entire vba.

Any good way to break into this? I don't want to go through a loop of 10000 items. I've seen enough, so I am ready to break into the code--to stop the code from running further.

How do I do this gracefully, without losing possibly any edits I may have made?

Maybe this can't be done, but worth knowing if it is possible.

user202729
  • 3,358
  • 3
  • 25
  • 36
Heap of Pinto Beans
  • 677
  • 2
  • 12
  • 23

3 Answers3

1

You can press Ctrl+Break to break into the code even when a MessageBox is being displayed.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Heap of Pinto Beans
  • 677
  • 2
  • 12
  • 23
  • 1
    It does work, but if the loop has been running for a while, it seems not to work. it might be okay in your situation as the msgbox will not let the loop go wild. – Davesexcel Oct 23 '18 at 15:03
0

if i interpret correctly your need :

"I don't want to go through a loop of 10000 items. I've seen enough, so I am ready to break into the code--to stop the code from running further."

Instead of a msgbox, you could use

Debug.print InformationYouNeed
stop

Then read the result and stop the code if you got the info you needed.

Can you provide the code you're using ?

AlexP
  • 13
  • 5
0

The easiest way is to set breakpoint on MsgBox line.

In order to do that just press F9, when you have cursor on that line.

Then the execution will stop before showing message box and you'll be able to inspect all variables as well as stop the code.

After setting breakpoint, the line wil l be highlighted with dark red: enter image description here

(insted of using F9 you can just click in place of the red circle - this will also set a brakpoint)

Other way is to show messagebox using WinApi, as descirbed here. But it's more complicated than using breakpoints. Code snippet for 64-bit windows:

Private Declare PtrSafe Function MessageBox _
        Lib "User32" Alias "MessageBoxA" _
           (ByVal hWnd As Long, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As Long) _
        As Long

Sub MsgBoxTests()
    ' Execution will show message box and stop on this line,
    ' at this point you will be able to stop execution, but still, you
    ' will need to dismiss messagebox
    MessageBox &O0, "This is a native Message Box", "My Box", vbOKOnly
End Sub
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69