' a bit more info for those who like me looking for help
' without Chip Pearson and many others my programming would still be at
' x=x+4
Option Explicit
'
' to list or sort procedure names
'
'
' on a spare sheet
'
Private Sub CommandButton1_Click()
Dim URA$, RaSort As Range, ModName$, VBC As VBComponent
Dim RangeStartAddress$: RangeStartAddress = "H11" ' any spare region
Set RaSort = Range(RangeStartAddress)
' sort and display needs 5 un-bordered columns so best done from spare worksheet
RaSort(0, 0).Resize(UsedRange.Rows.Count, 7).Clear
URA = UsedRange.Address ' tidy of used range
ModName = [c6]
' from cell C4 ... or whatever is needed name is needed
' OR ... to do all modules ... Skipping workbook try something like
'
'For Each VBC In ActiveWorkbook.VBProject.VBComponents
' Range("G11:N" & UsedRange.Rows.Count).Clear
' URA = UsedRange.Address
'Set RaSort = Range("h11")
'If Not (VBC.Name Like "Workbook") Then
' SortSUBLGFUN VBC.Name, RaSort
'End If
' Next VBC
SortSUBLGFUN ModName, RaSort
End Sub
'
' in a module
'
' sort the procedure names for a module
' Reference to VBE .. Microsoft Visual Basic for Applications Extensibility
' RaSort as some spare Range CurrentRegion
'
Sub SortSUBLGFUN(ComponentName$, RaSort As Range)
Dim LineI%, PBLI&, RowI&, RowOut&, LineStr$
Dim PLSG As vbext_ProcKind ' 0 Fun or Sub 1 Let 2 Set 3 Get
Dim ProcName$
Dim StartLineI&, CountLinesI&, LinesOfProc$
With ActiveWorkbook.VBProject.VBComponents(ComponentName).CodeModule
LineI = .CountOfDeclarationLines + 1
While LineI < .CountOfLines
PLSG = 0
While PLSG < 3 And LineI < .CountOfLines ' look for all types
On Error GoTo LookMore ' msny may not exist
ProcName = .ProcOfLine(LineI, PLSG)
CountLinesI = .ProcCountLines(ProcName, PLSG)
StartLineI = .ProcStartLine(ProcName, PLSG)
RowOut = RowOut + 1
RaSort(RowOut, 1) = ProcName
RaSort(RowOut, 2) = PLSG
RaSort(RowOut, 3) = StartLineI
RaSort(RowOut, 4) = CountLinesI
' the procedure can have blanks or comment lines at the top
' so start line is not always the Procedure body line
' the ProcBodyLine may be extended for over about 20 lines
' using the line-continuation char " _"
' so it looks a bit complex to find the actual line
PBLI = .ProcBodyLine(ProcName, PLSG)
LineStr = .Lines(PBLI, 1)
While Right(LineStr, 2) = " _" ' if extended get the other lines
PBLI = PBLI + 1
LineStr = Left(LineStr, Len(LineStr) - 2) & " " & .Lines(PBLI, 1)
Wend
RaSort(RowOut, 5) = LineStr
LineI = StartLineI + CountLinesI + 1
If LineI > .CountOfLines Then PLSG = 14 ' > 3
LookMore:
On Error GoTo 0
PLSG = PLSG + 1
Wend
LineI = LineI + 1
Wend
Set RaSort = RaSort.CurrentRegion
RaSort.Sort RaSort(1, 1), xlAscending
'
'bring each to the top from Z to A results in sorted alphabetically
'
For RowI = RaSort.Rows.Count To 1 Step -1
ProcName = RaSort(RowI, 1)
PLSG = RaSort(RowI, 2)
'
' since they have moved need to refind them before moving to top
'
CountLinesI = .ProcCountLines(ProcName, PLSG)
StartLineI = .ProcStartLine(ProcName, PLSG)
LinesOfProc = .Lines(StartLineI, CountLinesI)
.DeleteLines StartLineI, CountLinesI
.InsertLines .CountOfDeclarationLines + 1, LinesOfProc
Next RowI
End With
End Sub
'
' you may find the two below of interest
'
Sub TabsAscending()
Dim I&, J&
For I = 1 To Application.Sheets.Count
For J = 1 To Application.Sheets.Count - 1
If UCase$(Application.Sheets(J).Name) > UCase$(Application.Sheets(J + 1).Name) then
Sheets(J).Move after:=Sheets(J + 1)
End If
Next J
Next I
End Sub
Sub ResetCodeNames(WkWb As Workbook)
'Changes the codename conventional name gets rid of Sheet3 Sheet7 where they have been given a name
Dim VarItem As VBIDE.VBComponent
For Each VarItem In WkWb.VBProject.VBComponents
'Type 100 is a worksheet
If VarItem.Type = 100 And VarItem.Name <> "ThisWorkbook" Then
VarItem.Name = VarItem.Properties("Name").Value
End If
Next
End Sub
' hope it helps others