D
donbowyer
Excel 2003. Win XP
The subroutine below is called to programatically write code into a
worksheet object sheet that is added to the sheets collection with an add
sheet macro.
On first use, it hangs at the second line Set SheetCodeModule.
If I delete the new sheet and run the add sheet routine again, the
WriteCode() subroutine works fine and the code now appears in the sheet
object.
Sub WriteCode()
MySh = ActiveSheet.CodeName
Set SheetCodeModule = ThisWorkbook.VBProject.VBComponents(MySh).CodeModule
With SheetCodeModule
CodeLine = .CountOfLines + 1
.InsertLines CodeLine, "Sub Worksheet_Activate()" & Chr(13) & _
"Application.ScreenUpdating = False" & Chr(13) & _
"If MyNewSheet = ""Running"" Then" & Chr(13) & _
"MyNewSheet = ""NotRunning""" & Chr(13) & _
"Go To Line2" & Chr(13) & _
"End If" & Chr(13) & _
"Call Load" & Chr(13) & _
"Line2:" & Chr(13) & _
"End Sub"
End With
Set SheetCodeModule = Nothing
End Sub
Any suggestions as to what is wrong would be welcome.
The subroutine below is called to programatically write code into a
worksheet object sheet that is added to the sheets collection with an add
sheet macro.
On first use, it hangs at the second line Set SheetCodeModule.
If I delete the new sheet and run the add sheet routine again, the
WriteCode() subroutine works fine and the code now appears in the sheet
object.
Sub WriteCode()
MySh = ActiveSheet.CodeName
Set SheetCodeModule = ThisWorkbook.VBProject.VBComponents(MySh).CodeModule
With SheetCodeModule
CodeLine = .CountOfLines + 1
.InsertLines CodeLine, "Sub Worksheet_Activate()" & Chr(13) & _
"Application.ScreenUpdating = False" & Chr(13) & _
"If MyNewSheet = ""Running"" Then" & Chr(13) & _
"MyNewSheet = ""NotRunning""" & Chr(13) & _
"Go To Line2" & Chr(13) & _
"End If" & Chr(13) & _
"Call Load" & Chr(13) & _
"Line2:" & Chr(13) & _
"End Sub"
End With
Set SheetCodeModule = Nothing
End Sub
Any suggestions as to what is wrong would be welcome.