Article ID: 128374
Article Last Modified on 10/11/2006
Sub CountSubs()
Dim Count As Integer, Filenum As Integer, textline As String
' Initialize the count of procedures to zero.
Count = 0
' Save Module1 as a text file called TEMPFILE.TXT.
Modules("Module1").Select
ActiveWorkbook.SaveAs "TEMPFILE.TXT", xlText
' Retrieve the next available file number as FileNum and then open
' the text file with the file number.
Filenum = FreeFile()
Open "TEMPFILE.TXT" For Input As #Filenum
On Error GoTo CloseFile
' Read each line of the text file until the end of the file is
' reached. If the first 3 characters of the line of text is equal to
' "Sub" after trimming excesses spaces, then increment count.
Do While Not (EOF(Filenum))
Line Input #Filenum, TextLine
If Left(LTrim(TextLine), 3) = "Sub" Then Count = Count + 1
Loop
' Close the file.
Close #Filenum
' Display the count for the number of subs in the module sheet
' and Exit this procedure.
MsgBox "There are " & Count & " Subs in Module1 of the " & _
"active workbook."
Exit Sub
CloseFile:
' Close the file and display a message that an error occurred.
Close Filenum
MsgBox "An error occurred"
End Sub
NOTE: If you want the macro to account for private, public, and static Sub
statements as well, replace the following line of the macro:
If Left(LTrim(TextLine), 3) = "Sub" Then Count = Count + 1with this code:
If Left(LTrim(TextLine), 3) = "Sub" Then Count = Count + 1 If Left(LTrim(TextLine), 11) = "Private Sub" then Count = Count + 1 If Left(LTrim(TextLine), 10) = "Public Sub" Then Count = Count + 1 If Left(LTrim(TextLine), 10) = "Static Sub" Then Count = Count + 1
Sub DisplaySubs()
Dim Filenum As Integer, textline As String
Dim leftparen As Integer, macroname As String
' Save Module1 as a text file called TEMPFILE.TXT.
Modules("Module1").Select
ActiveWorkbook.SaveAs "TEMPFILE.TXT", xlText
' Retrieve the next available file number as FileNum and then open
' the text file with the file number.
Filenum = FreeFile()
Open "TEMPFILE.TXT" For Input As #Filenum
On Error GoTo CloseFile
' Read each line of the text file until the end of the file is
' reached. If the first 3 characters of the line of text is equal to
' "Sub" after trimming excess spaces, get the macro name and display
' it.
Do While Not (EOF(Filenum))
Line Input #Filenum, TextLine
If Left(LTrim(TextLine), 3) = "Sub" Then
LeftParen = InStr(1, TextLine, "(")
macroname = Mid(Left(TextLine, LeftParen - 1), 5)
MsgBox macroname
End If
Loop
' Close the file.
Close #Filenum
Exit Sub
CloseFile:
' Close the file and display a message that an error occurred.
Close Filenum
MsgBox "An error occurred"
End Sub
Additional query words: 5.00a 5.00c list XL5 XL7 XL
Keywords: kbcode kbhowto kbprogramming KB128374