Vba excel 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.

David Zemens's user avatar

David Zemens

52.8k11 gold badges79 silver badges129 bronze badges

asked Nov 13, 2015 at 17:13

Cloude's user avatar

8

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.

answered Sep 30, 2019 at 21:47

1

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.

answered Oct 25, 2019 at 1:25

ChrisB's user avatar

ChrisBChrisB

2,8845 gold badges31 silver badges58 bronze badges

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:

  1. From the VBE Debug menu, «Compile VBA Project», it’s worth a shot.
  2. Delete the line entirely. Run the code. Then put the line back in and try again with the breakpoint.
  3. Add a DoEvents statement after the Sheets.Add
  4. Use a MsgBox instead of a breakpoint on a Debug.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)
  5. Put a breakpoint on Sheets.Add instead; practically speaking, there’s no reason to put the breakpoint on a Print statement if you can just put it on the preceding line.
  6. 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.

answered Nov 13, 2015 at 18:53

David Zemens's user avatar

David ZemensDavid Zemens

52.8k11 gold badges79 silver badges129 bronze badges

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.

answered Dec 12, 2018 at 17:49

Pragmateek's user avatar

PragmateekPragmateek

13.1k9 gold badges73 silver badges108 bronze badges

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. :)

answered Sep 21, 2021 at 8:04

Spinner's user avatar

SpinnerSpinner

1,0661 gold badge5 silver badges15 bronze badges

1

This has happened to me multiple times and this solution works for me.

  1. Run a different macro within the same VBA project.
  2. Then go back and run the same macro that is causing the pop-up message to appear. The message should no longer appear.

answered Feb 23, 2022 at 23:38

Developer's user avatar

DeveloperDeveloper

7417 silver badges12 bronze badges

Using Excel 2010; Windows 7 all up to date; 2gigs Ram; 2gigs Centrino Duo

VBA code below creates a password query form in a VBA project.  After creating the form; then obtaining

the user input, i.e. PasswordVar; the code then deletes the form. 

It works!! … BUT there is an annoying issue: 

When the code-line («.InsertLines X + 2, «PasswordVar = Authorization.Value») is executed
I get a VBA error message ‘Can’t enter break mode at this time’
As you can see, I have attempted a delay i.e. Application.Wait (Now + TimeValue(«0:00:02»))
to no avail.  BTW, it makes no difference if the delay is 1 thru 10+ seconds.

I realize that some issue is stopping (breaking) during calculation/processing.  That said, I have

no idea Where or How to fix it.

Any thoughts or workarounds?

Thanks in advance!!!

Dennis

************************************************************************************************

Public PasswordVar as String

Sub TempPasswordForm()
   ‘NOTE: Must place «Public PasswordVar as String» st the top of this VBA Code Module
   ‘  to cause PasswordVar to survive this «Sub» thru to the «Calling» program
   ‘
   Dim TempForm As Object
   Dim NewTextBox As MSForms.TextBox
   Dim X As Integer

  
   Application.ScreenUpdating = False

  
   Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
   FormName = TempForm.Name

  
   With TempForm
      .Properties(«Caption») = «Authorization Information»
      .Properties(«Width») = 200
      .Properties(«Height») = 100
   End With

  
   Set NewTextBox = TempForm.Designer.Controls.Add(«Forms.textbox.1»)

  
   With NewTextBox
      ‘Remove PasswordChar
      ‘textBox1.PasswordChar = «»
      ‘and to go back to asterixes is
      ‘textBox1.PasswordChar = «*»
      .Name = «Authorization»
      .Top = 12
      .Left = 6
      .Width = 186
      .Height = 20
      .Font.Size = 14
      .Font.Name = «Tahoma»
      .BorderStyle = fmBorderStyleSingle
      .SpecialEffect = fmSpecialEffectFlat
      .PasswordChar = «*»
   End With

  
   Set NewButton = TempForm.Designer.Controls.Add(«forms.CommandButton.1»)

  
   With NewButton
       .Caption = «Click Me»
       .Height = 24
       .Left = 60
       .Top = 42
       .Width = 72
   End With

   ‘Next section creates a Temporary Worksheet Code Module which disappears when this procedure
   ‘ closes.  Note that PasswordVar is initialized next below and carried back to the «Calling» procedure

   With TempForm.CodeModule
       X = .CountOfLines
      .InsertLines X + 1, «Sub CommandButton1_Click()»
      .InsertLines X + 2, «PasswordVar = Authorization.Value»             ‘<<Error occurs HERE!
      .InsertLines X + 5, «Unload Me»
      .InsertLines X + 6, «End Sub»
   End With
   ‘Two second time delay counter the «VBA error message ‘Can’t enter break mode at this time'»
   Application.Wait (Now + TimeValue(«0:00:02»))

   VBA.UserForms.Add(FormName).Show
   ‘Delete the temporary TempForm created above
   ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm

End Sub

  1. 04-08-2009, 10:22 PM


    #1

    jp001 is offline


    Registered User


    «Can’t enter break mode at this time» error

    Can someone please tell me why I get a «Can’t enter break mode at this time» message when stepping through the following code (using the F8 key) at the ‘With NewButton’ line?

    (This is code from a Walkenbach book, and it runs fine if I don’t step through it. I’m using Excel 2007 btw) Thanks!

    Last edited by jp001; 04-16-2009 at 10:16 PM.


  2. 04-08-2009, 10:38 PM


    #2

    re: «Can’t enter break mode at this time» error

    I can’t tell you the reason, but there is some code that you can’t step through. Another is when you modify code programmatically in the VBE. If it works fine, carry on.

    Entia non sunt multiplicanda sine necessitate


  3. 04-09-2009, 08:18 AM


    #3

    jp001 is offline


    Registered User


    re: «Can’t enter break mode at this time» error


  4. 04-09-2009, 11:54 AM


    #4

    re: «Can’t enter break mode at this time» error

    When you’re stepping through the code, just before that line, put your cursor on the line after the «End With» and «Step to cursor (Ctrl + F8)»


  5. 04-09-2009, 04:22 PM


    #5

    re: «Can’t enter break mode at this time» error

    Hello jp001,

    When OLE objects are created, they are created outside the Excel process using the OLE server. VBA must yield until the object has been created and included in Excel. Once this happens, the OLE server returns back to the VBA process. VBA can only enter break mode while the Excel process is active.

    Sincerely,
    Leith Ross

    Remember To Do the Following….

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.

    2. Thank those who have helped you by clicking the Star below the post.

    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

    Old Scottish Proverb…
    Luathaid gu deanamh maille! (Rushing causes delays!)


  6. 04-11-2009, 09:19 AM


    #6

    jp001 is offline


    Registered User


    re: «Can’t enter break mode at this time» error

    Thanks for the help guys!


  • #1

Hi, I am trying to create code using the VBA .CreateEventProc to create a worksheet_SElection change event. For some reason, anytime I try to run it, it tells me that I can’t enter break mode, but I didn’t even know I was trying to do that. Here is the code that I have. As far as I can tell, it creates the correct code in the event handler for the sheet, but it crashes right after. It says «Can’t enter break mode at this time,» then is followed by an automation error, then excel crashes. I have no idea what is causing the problem. It works if I run the code in the sub function that I created, but never if I run it using my program.

Here is the code:

Code:

Sub Create_Event_Procedure()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim LineNum As Long
    Const DQUOTE = """" ' one " character

    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents(ThisWorkbook.Sheets("Error_Resolution").CodeName)
    Set CodeMod = VBComp.CodeModule
    
    With CodeMod
        LineNum = .CreateEventProc("SelectionChange", "Worksheet")
        On Error Resume Next
        LineNum = LineNum + 1
        .InsertLines LineNum, "     If (Target.Value = " & DQUOTE & "Y" & DQUOTE & "Or Target.Value =" & DQUOTE & "Yes" & DQUOTE & " Or Target.Value = " & DQUOTE & "yes" & DQUOTE & "Or Target.Value = " & DQUOTE & "y" & DQUOTE & "Or Target.Value = " & DQUOTE & "YES" & DQUOTE & ") Then"
        LineNum = LineNum + 1
        .InsertLines LineNum, "     Target.Offset(0, 1).FormulaR1C1 =" & DQUOTE & "Empty" & DQUOTE
        LineNum = LineNum + 1
        .InsertLines LineNum, "     Target.Offset(0, 2).FormulaR1C1 =" & DQUOTE & "N/A" & DQUOTE
        LineNum = LineNum + 1
        .InsertLines LineNum, "     Target.Offset(0, 3).FormulaR1C1 =" & DQUOTE & "N/A" & DQUOTE
        LineNum = LineNum + 1
        .InsertLines LineNum, "     Target.Offset(0, 4).FormulaR1C1 =" & DQUOTE & "N/A" & DQUOTE
        LineNum = LineNum + 1
        .InsertLines LineNum, "     Target.Offset(0, 5).FormulaR1C1 =" & DQUOTE & "N/A" & DQUOTE
        LineNum = LineNum + 1
        .InsertLines LineNum, "End If"
        
    End With

End Sub

Like this post? Please share to your friends:
  • Vba excel call shell
  • Vba excel calculate all
  • Vba excel byref что это
  • Vba excel borders linestyle
  • Vba excel boolean это что