Hi,
Once again I find myself having to edit some code in around 52 workbooks, each workbook has one sheet. I was able to use this code below to delete the problem worksheet code. However what I want now is to add some code back into the worksheet. I have seen several examples on how to add Mods and placing code into mods and even how to add code into Thisworkbook. but havent found anything about adding in sheet code.
Anyway does anyone know a way to add a private sub routine into a worksheet?
Here is what I used to remove the code. Can someone help modify this to add code back in?
Sub DeleteProcedureFromModule()
Const vbext_pk_Proc As Integer = 0
Const vbext_ct_Document As Integer = 100
Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim StartLine As Long
Dim NumLines As Long
Dim ProcName As String
Dim ws As Object
Set VBProj = ActiveWorkbook.VBProject
For Each ws In ActiveWorkbook.Worksheets
Set VBComp = VBProj.vbcomponents(ws.CodeName)
Set CodeMod = VBComp.CodeModule
ProcName = "CommandButton3_Click"
With CodeMod
On Error Resume Next
StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
If Err.Number = 0 Then
NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
.DeleteLines StartLine:=StartLine, Count:=NumLines
ElseIf Err.Number = 35 Then
Err.Clear
End If
End With
Next
End Sub
Thanks as always, Mike
Bookmarks