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