View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
keithb keithb is offline
external usenet poster
 
Posts: 44
Default Excel crashes when programming to the VBA Editor

Here are three code fragments. The first one works propely, the second two
cause Excel to crash. Can someone tell me what I am doing wrong? Thanks

The following code works properly:
Dim VBCodeMod As CodeModule
Dim LineNum As Long
Set VBCodeMod =
Workbooks(wb).VBProject.VBComponents("ThisWorkbook ").CodeModule
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub Workbook_Open()" & Chr(13) & _
"setLookupList" & Chr(13) & _
"End Sub"
End With

This code causes excel to crash:
Dim VBCodeMod As CodeModule
Dim LineNum As Long
Set VBCodeMod =
Workbooks(wb).VBProject.VBComponents("Sheet1").Cod eModule
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"'Private Sub Worksheet_Change(ByVal Target As Range)" & Chr(13) & _
"'doIt Target" & Chr(13) & _
"End Sub"
End With

Similarly, this code also causes excel to crash:
Dim StartLine As Long
With Workbooks(wb).VBProject.VBComponents("Sheet1").Cod eModule
StartLine = .CreateEventProc("Change", "Worksheet") + 1
.InsertLines StartLine, _
"dotIt Target"
End With