'Written: July 04, 2011
'Author: Leith Ross
'Summary: Removes all procedure code from a given module for a given object.
Sub RemoveCode(ByVal ModuleName As String, ByVal ObjectName)
Dim I As Long
Dim LineCount As Long
Dim VBproc As String
Dim VBproj As Object
Set VBproj = ThisWorkbook.Application.VBE.ActiveVBProject
With VBproj.VBComponents(ModuleName).CodeModule
For I = .CountOfLines To 1 Step -1
VBproc = .ProcOfLine(I, 0)
LineCount = .ProcCountLines(VBproc, 0)
If InStr(1, VBproc, ObjectName) Then
.DeleteLines .ProcStartLine(VBproc, 0), LineCount
End If
I = I - LineCount + 1
Next I
End With
End Sub
Hello lolo,
This may save you some time and hair pulling. Copy this code into a standard VBA module in your workbook. You can then call RemoveCode ActiveSheet.Name, "CommandButton1" from your macro.
Sincerely,Code:'Written: July 04, 2011 'Author: Leith Ross 'Summary: Removes all procedure code from a given module for a given object. Sub RemoveCode(ByVal ModuleName As String, ByVal ObjectName) Dim I As Long Dim LineCount As Long Dim VBproc As String Dim VBproj As Object Set VBproj = ThisWorkbook.Application.VBE.ActiveVBProject With VBproj.VBComponents(ModuleName).CodeModule For I = .CountOfLines To 1 Step -1 VBproc = .ProcOfLine(I, 0) LineCount = .ProcCountLines(VBproc, 0) If InStr(1, VBproc, ObjectName) Then .DeleteLines .ProcStartLine(VBproc, 0), LineCount End If I = I - LineCount + 1 Next I End With End Sub
Leith Ross