'Can't enter break mode at this time
This has been happening increasingly, when I have a sheets.add or sheets.delete in excel VBA. After searching and searching I finally found the official Microsoft support page on it. My question is, does anyone know why I could have stepped through this code just fine over and over, and then all of a sudden it starts doing what Microsoft says it would always do, and is there a way to fix it?
Sub foo()
Sheets.add
debug.print "sheet added" 'breakpoint here
End sub
It's as simple as that. You won't be able to recreate it, because the issue I'm asking about is the fact that it doesn't happen at first. It works just fine over and over then randomly presents the error described in the linked Microsoft support page.
Solution 1:[1]
Check if Microsoft Visual Basic for Applications Extensibility is being referenced in the project.
You can check that in the Tools/References Menu on the Visual Basic window of the project.
Referencing Visual Basic for Applications Extensibility prevents the program having its execution suspended:
Excel helps says specifically:
A change was made programmatically to the project using the extensibility (add-in) object model. This prevents the program from having execution suspended. You can continue running, or end execution, but can't suspend execution.
You are unable to step through code when making changes to the project (dynamically eg using InsertLine etc). the code can be run but not stepped through.
Solution 2:[2]
Deleting certain objects including ActiveX objects actually changes the VB project. It took me some time to realize that the following line of code prevented the VBE from entering break mode:
Excel.ActiveSheet.DrawingObjects.Delete
If you can identify the code causing the issue, and the order of operations isn't important, move it to the end of your script.
Solution 3:[3]
Here are a few suggestions which are not fool-proof,
Firstly, verify that the error does not occur if a breakpoint is not set.
If it doesn't, try a few other things:
- From the VBE Debug menu, "Compile VBA Project", it's worth a shot.
- Delete the line entirely. Run the code. Then put the line back in and try again with the breakpoint.
- Add a
DoEventsstatement after theSheets.Add - Use a
MsgBoxinstead of a breakpoint on aDebug.Print. With the message box displayed, attempt to manually break using ctrl+fn+End. (At this point, "breaking" isn't necessary but it would be interesting to see whether you can break this way) - Put a breakpoint on
Sheets.Addinstead; practically speaking, there's no reason to put the breakpoint on aPrintstatement if you can just put it on the preceding line. - Are there any Addins? If so, disable all of them and re-enable one at a time, testing to see which one may contribute to the error.
Solution 4:[4]
Yet another Excel/VBA glitch.
When it happens to me when I click a button running a macro:
- I first try to directly run the macro from VBE,
- if it fails, then I put a breakpoint at the first instruction of the macro,
- if it still fails, I try both,
- or, after clicking the button and breaking on the first breakpoint, I do a single step (
SHIFT F8) and then I can let debug run freely as usual (F5).
And so far I don't get this error anymore.
Probably not foolproof either but worth a try.
Solution 5:[5]
Ran into the same issue, and (as far as I can tell) the only relevant answer here is Answer 5 (i.e. the one provided by Chrisb).
I've been working with vba for (too many) years now, and never encountered this until I had a project that needed vba to delete ActiveX controls. In my case, the 'ActiveX controls' were a spurious result of data copied in from a web page.
Additionally, there appears to be a way around the issue. Using the following code (versus, e.g. deleting the ActiveX as a shape), seems to circumvent the issue:
On Error Resume Next
ActiveSheet.OLEObjects.Visible = True
ActiveSheet.OLEObjects.Delete
On Error GoTo 0
I say 'appears' and 'seems' above as implementing the above solved the issue for me. However, before I implemented same, I had made other code changes and I have not yet fully regression tested for all possible other reasons the problem was resolved. :)
Solution 6:[6]
This has happened to me multiple times and this solution works for me.
- Run a different macro within the same VBA project.
- Then go back and run the same macro that is causing the pop-up message to appear. The message should no longer appear.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ChrisB |
| Solution 3 | David Zemens |
| Solution 4 | |
| Solution 5 | |
| Solution 6 |
