1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
Private Sub Noms_Modules()
Dim CollectionModule As Object
Dim Module As Object
Dim Ligne As String
Dim Pos As Integer
Dim I As Integer
Dim J As Integer
Dim K As Integer
Dim Proc As Boolean
Dim Op_Explicit As Boolean
On Error Resume Next
Set CollectionModule = ActiveWorkbook.VBProject.VBComponents
For Each Module In CollectionModule
'le module où est mis ce code doit s'appeller "Module1" si on veux
'éviter qu'il soit pris en compte sinon, adapter le nom ici
If Module.Name <> "Module1" Then
K = K + 1
'inscrit le nom du module en gras, fond de cellule en jaune
'et aligné à gauche
Cells(K, 1) = Module.Name
Cells(K, 1).Font.Bold = True
Cells(K, 1).Interior.ColorIndex = 44
Cells(K, 1).HorizontalAlignment = xlLeft
'mets le drapeau à False pour récupérer les variables en tête de module
Proc = False
'boucle sur les lignes de code
For I = 1 To Module.CodeModule.CountOfLines
'défini un drapeau si la demande de déclaration des variables est explicitée
If Left(Ligne, 15) = "Option Explicit" Then
Op_Explicit = True
End If
'supprime les espaces
Ligne = Trim(Module.CodeModule.Lines(I, 1))
'variables en tête de module
If Left(Ligne, 3) = "Dim" Or _
Left(Ligne, 6) = "Public" Or _
Left(Ligne, 6) = "Global" Or _
Left(Ligne, 7) = "Private" Or _
Left(Ligne, 6) = "Static" Then
If Proc = False And InStr(Ligne, "Sub") = 0 And InStr(Ligne, "Function") = 0 Then
K = K + 1
'variables alignées à droite
Cells(K, 1) = Ligne
Cells(K, 1).Font.Bold = False
Cells(K, 1).HorizontalAlignment = xlRight
End If
End If
If InStr(Ligne, "Sub") <> 0 Or _
InStr(Ligne, "Function") <> 0 And _
InStr(Ligne, "End Sub") = 0 And _
InStr(Ligne, "End Function") = 0 Then
Proc = True
K = K + 1
'inscrit le nom de la proc en gras, fond de cellule en vert
Cells(K, 1) = Left(Ligne, InStr(Ligne, "(") - 1)
Cells(K, 1).Font.Bold = True
Cells(K, 1).Interior.ColorIndex = 43
Cells(K, 1).HorizontalAlignment = xlLeft
'continue le passage sur les lignes à partir de la ligne en cours
For J = I To Module.CodeModule.CountOfLines
'toujours sans espace
Ligne = Trim(Module.CodeModule.Lines(J, 1))
'
If Left(Ligne, 3) = "Dim" Or Left(Ligne, 6) = "Static" Then
K = K + 1
'variables alignées à droite
Cells(K, 1) = Ligne
Cells(K, 1).HorizontalAlignment = xlRight
End If
'si on rencontre la fin de la proc, continu la recherche des proc
'à la ligne suivante
If Left(Ligne, 7) = "End Sub" Or _
Left(Ligne, 12) = "End Function" Then
I = J
Exit For
End If
Next J
End If
Next I
'si pas de déclaration explicite, message sinon, remet le drapeau à False
If Op_Explicit = False Then
MsgBox "Attention, la déclaration explicite des variables n'a pas été demandée en tête du module '" & Module.Name & "' !" & vbCrLf _
& "Il se peut que certaines variables ne soient pas déclarées explicitement, donc la procédure ne les détectera pas.", _
vbExclamation, _
"Déclaration des variables."
Else
Op_Explicit = False
End If
End If
Next Module
Set CollectionModule = Nothing
Set Module = Nothing
End Sub |