"can't enter break mode at this time"

shawleigh17

Board Regular
Joined
Nov 16, 2007
Messages
79
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
shawleigh17,
Was running through unanswered posts and found this one from you.
Were you able to figure this out?

When you tried to execute your code, were you just hitting "Run" or were you stepping through it in "Break" mode.

The only way I could duplicate your error was to "Step Into" the code in "Break" mode
Your code ran perfectly for me as long as I just told it to "Run".

Just curious to see where you are at on this?

Gary
 
Upvote 0
Yes, I got it to work. The problem came after I ran the code, I think it kicked me into break mode, so whenever I tried to run anything after this (in the code), it told me that I couldn't do it, because it was in break mode. I just moved this code to run after everything else, and it ran fine. Thanks for replying!\

-Shawna
 
Upvote 0
Glad you got it working!
I have done quite a bit of writing code to the modules and sheets in an app.
It was to add the code after auto creating a new workbook.
One workbook creating another to replace itself.

Gary
 
Upvote 0

Forum statistics

Threads
1,223,636
Messages
6,173,483
Members
452,516
Latest member
archcalx

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