Jon,
Here is an amended version.
One question, is it an event procedure you are deleting/adding as that is
different?
Const vbext_pk_Proc = 0
Sub Control()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
DeleteProcedure sh.CodeName, "MyProc"
AddProcedure sh.CodeName, "MyProc"
Next sh
End Sub
'----------------------------------------------------------------
Sub DeleteProcedure(module As String, proc As String)
'----------------------------------------------------------------
Dim oCodeModule As Object
Dim iStart As Long
Dim cLines As Long
Set oCodeModule =
ActiveWorkbook.VBProject.VBComponents(module).CodeModule
With oCodeModule
On Error GoTo dp_err:
iStart = .ProcStartLine(proc, vbext_pk_Proc)
cLines = .ProcCountLines(proc, vbext_pk_Proc)
.DeleteLines iStart, cLines
On Error GoTo 0
Exit Sub
End With
dp_err:
If Err.Number = 35 Then
MsgBox "Procedure does not exist"
End If
End Sub
'----------------------------------------------------------------
Sub AddProcedure(module As String, proc As String)
'----------------------------------------------------------------
Dim oCodeModule As Object
Dim cLines As Long
Set oCodeModule =
ActiveWorkbook.VBProject.VBComponents(module).CodeModule
With oCodeModule
cLines = .CountOfLines + 1
.InsertLines cLines, _
"" & vbCrLf & _
"Sub " & proc & "()" & vbCrLf & _
" Msgbox ""Here is the new procedure"" " & vbCrLf & _
" Call JonDidNotMessUp" & vbCrLf & _
"End Sub"
End With
End Sub
--
HTH
RP
(remove nothere from the email address if mailing direct)