'=================================================================================
'- MACRO TO DELETE *SUBROUTINES* FROM A VBA CODE MODULE
'- Check Tools/References/"Microsoft Visual Basic for Applications Extensibility"
'- Brian Baulsom August 2007
'=================================================================================
'=================================================================================
'- MAIN ROUTINE
'=================================================================================
Sub DeleteSubroutines()
DeleteSubroutine "Module1", "computation1"
DeleteSubroutine "Module1", "computation2"
End Sub
'========= END OF MAIN ROUTINE ===================================================
'=================================================================================
'- SUBROUTINE : CALLED FROM MAIN ROUTINE
'=================================================================================
Private Sub DeleteSubroutine(ModuleName, SubName)
Dim MyModule As Object
Dim MyLineNumber As Integer
Dim MyLine As String
Dim StartLine As Integer
Dim EndLine As Integer
Dim MySubLines As Integer
'-----------------------------------------------------------------------------
Set MyModule = ActiveWorkbook.VBProject.vbComponents(ModuleName).codemodule
MyLineNumber = 1
With MyModule
'-----------------------------------------------------------------------------
'- Find subroutine
'-----------------------------------------------------------------------------
For MyLineNumber = 1 To .countoflines
MyLine = .Lines(MyLineNumber, 1)
If InStr(1, MyLine, SubName, vbTextCompare) > 0 Then
StartLine = MyLineNumber
Exit For
End If
Next
'--------------------------------------------------------
'- check subroutine found
If MyLineNumber >= .countoflines Then
MsgBox ("Cannot find Sub " & SubName & "()" & vbCr _
& "in module '" & ModuleName & "'")
Exit Sub
End If
'-----------------------------------------------------------------------------
'- Find End Sub
'-----------------------------------------------------------------------------
While InStr(1, MyLine, "End Sub", vbTextCompare) = 0
MyLineNumber = MyLineNumber + 1
MyLine = .Lines(MyLineNumber, 1)
Wend
EndLine = MyLineNumber + 1
'-----------------------------------------------------------------------------
'- delete lines
'-----------------------------------------------------------------------------
MySubLines = EndLine - StartLine
.DeleteLines StartLine, MySubLines
End With
'----------------------------------------------------------------------------------
MsgBox ("Deleted Sub " & SubName & " ( )" & vbCr _
& "from module '" & ModuleName & "'" & vbCr & "= " & MySubLines & " lines.")
End Sub
'----------- end of sub routine --------------------------------------------------------