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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| Sub Mise_en_forme()
'préparatoire
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'définition des variables
Dim MonChemin As String, MonFichier As String
Dim MEF As Workbook
Dim NOUVEAU As Workbook
'chemin d'accès
MonChemin = Range("CHEMIN")
MonChemin = InputBox("Veuillez saisir le chemin d'accès", "REPERTOIRE SOURCE", Range("CHEMIN"))
Range("CHEMIN") = MonChemin
MonFichier = Dir(MonChemin & "\*")
'Boucle sur les fichiers du dossier
'----------------------------------
c = 0 'compteur
Do While MonFichier <> ""
If Left(MonFichier, 22) = "relances_qualitatives_" Then
Workbooks.Open Filename:=MonChemin & "\" & MonFichier, ReadOnly:=False, UpdateLinks:=False
Set MEF = ActiveWorkbook 'déclaration du fichier à mettre en forme
c = c + 1
'Taille du tableau
MEF.Worksheets("ANOMALIE_VARIATION").Activate
'insérer une colonne
Range("A:A").Insert
Range("A1") = "Code_bce"
Range("A1").Select
Fin_Col = Selection.End(xlToRight).Column
Range("B1").Select
Fin_Row = Selection.End(xlDown).Row
'écrire le code de la série
code = Split(MonFichier, "_")
Range(Cells(2, 1), Cells(Fin_Row, 1)).Select
Selection = code(3)
'Mise en forme
Range(Cells(1, 1), Cells(1, Fin_Col)).Select 'coloration de la première ligne
Selection.Font.Bold = True
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Range(Cells(1, 1), Cells(Fin_Col, Fin_Col)).EntireColumn.Select
Range(Cells(1, 1), Cells(Fin_Col, Fin_Col)).EntireColumn.AutoFit
MEF.Save
MEF.Close
End If
MonFichier = Dir()
Loop
MonFichier = Dir(MonChemin & "\*")
CIB_prec = ""
CIB_act = ""
n = 0 'compte le nombre de fichiers portant sur le même CIB
c = 0 'compte le nombre de fichier
Do While MonFichier <> ""
If Left(MonFichier, 22) = "relances_qualitatives_" Then
CIB = Split(MonFichier, "_")
CIB_act = CIB(2)
c = c + 1
Set MEF = Workbooks.Open(Filename:=MonChemin & "\" & MonFichier, ReadOnly:=False, UpdateLinks:=False) 'déclaration du fichier à mettre en forme
If CIB_act <> CIB_prec Then
'on ferme le nouveau classeur précédent (sauf lors de la première boucle car il n'existe pas)
If n <> 0 Then
NOUVEAU.SaveAs (MonChemin & "\Classeur" & CStr(c))
NOUVEAU.Close
End If
n = 1
Set NOUVEAU = Workbooks.Add
'Taille du tableau
MEF.Worksheets("ANOMALIE_VARIATION").Activate
Range("A1").Select
Fin_Col = Selection.End(xlToRight).Column
Range("B1").Select
Fin_Row = Selection.End(xlDown).Row
MEF.Worksheets("ANOMALIE_VARIATION").Range(Cells(1, 1), Cells(Fin_Row, Fin_Col)).Copy Destination:=NOUVEAU.Worksheets("Feuil1").Range("A1")
Else
n = n + 1
'Taille du tableau
MEF.Worksheets("ANOMALIE_VARIATION").Activate
Range("A1").Select
Fin_Col = Selection.End(xlToRight).Column
Range("B1").Select
Fin_Row = Selection.End(xlDown).Row
NOUVEAU.Worksheets("Feuil1").Activate
Range("A1").Select
Fin_Col_nouv = Selection.End(xlToRight).Column
d = 0
m = 1
Fin_Row_nouv = 1
Do While m < n
'Taille du tableau
NOUVEAU.Worksheets("Feuil1").Activate
Cells(Fin_Row_nouv + d, 1).Select
Fin_Row_nouv = Selection.End(xlDown).Row
d = 2
m = m + 1
Loop
MEF.Worksheets("ANOMALIE_VARIATION").Range(Cells(1, 1), Cells(Fin_Row, Fin_Col)).Copy Destination:=NOUVEAU.Worksheets("Feuil1").Range(Cells(Fin_Row_nouv + 2, 1), Cells(Fin_Row_nouv + 2, 1))
End If
MEF.Close
End If
CIB_prec = CIB(2)
MonFichier = Dir()
Loop
NOUVEAU.SaveAs (MonChemin & "\Classeur" & CStr(c))
NOUVEAU.Close
'message d'info et fin
MsgBox c & " fichiers traités"
Application.Goto Range("A1"), True
Range("A4").Select
End Sub |
Partager