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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
| Option Explicit
Sub TEST_TRI()
'
' TEST_TRI Macro
' Macro enregistrée le 10/05/2016 par lycée
'Suppression TCD
Sheets("Tableau").Activate
Range("A1:Q300").Select
Range("Q300").Activate
Selection.Delete
'Suppresion Liste
Sheets("Liste").Activate
Range("A1:L400").Select
Range("L400").Activate
Selection.Delete
'Définition des critères pour CITEC
Sheets("Génération").Activate
If ([B1] = 1 Or [B1] = 2) And [C1] = 3 And ([D1] = 1 Or [D1] = 2) And ([E1] = 1 Or [E1] = 2) And ([K1] = 1 Or [K1] = 2) And ([L1] = 1 Or [L1] = 2) Then
'Filtre élaboré pour trier la BD élève
Sheets("BD Eleves").Range("A1:L400").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Génération").Range("T4:AE5"), CopyToRange:=Sheets("Liste").Range( _
"A1:L1"), Unique:=False
Sheets("Liste").Select
'Tri de la feuille Liste, par Option 4 puis 5
Cells.Select
Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Key2:=Range("H2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
'Déclaration des variables
Range("A1").Select
Dim wb As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim rngPT As Range
'Optimisation (Gel Affichage)
Application.ScreenUpdating = False
'Initialisation des variables
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Liste")
Set rngPT = ws.Cells(1).CurrentRegion 'Données sources du TCD
Set ws2 = wb.Worksheets("Tableau")
'Suppression TCD
ws2.PivotTables(1).TableRange2.Clear
'Création du cache de TCD (à partir de rngPT)
Set PTCache = wb.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=rngPT)
'Création du TCD en feuille 'Tableau' nommé TCD_1
Set pt = PTCache.CreatePivotTable _
(tabledestination:=ws2.Cells(6, 2), _
TableName:="TCD_1", _
defaultversion:=xlPivotTableVersion10)
With pt
'Calcul TCD manuel (Optimisation)
.ManualUpdate = True
'Ajout des étiquettes de lignes et colonnes
.AddFields RowFields:=Array("OPTION 4", "OPTION ECO"), _
ColumnFields:="SEXE"
'Ajout champ valeurs
With .PivotFields("NOM")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Caption = "NB NOMS"
End With
'Calcul automatique (affiche le TCD)
.ManualUpdate = False
End With
wb.ShowPivotTableFieldList = False
With ws2
.Activate
.[A1].Select
End With
Set rngPT = Nothing
Set pt = Nothing
Set PTCache = Nothing
Set ws2 = Nothing: Set ws = Nothing
Set wb = Nothing
Sheets("Tableau").Activate
'Définition des critères pour SC-IG
Sheets("Génération").Activate
ElseIf ([B1] = 1 Or [B1] = 2) And [C1] = 9 And ([D1] = 1 Or [D1] = 2) And ([E1] = 1 Or [E1] = 2) And ([K1] = 1 Or [K1] = 2) And ([L1] = 1 Or [L1] = 2) Then
'Suppression TCD
Sheets("Tableau").Activate
Range("A1:Q300").Select
Range("Q300").Activate
Selection.Delete
'Suppresion Liste
Sheets("Liste").Activate
Range("A1:L400").Select
Range("L400").Activate
Selection.Delete
'Filtre élaboré pour trier la BD élève
Sheets("BD Eleves").Range("A1:L400").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Génération").Range("T7:AE8"), CopyToRange:=Sheets("Liste").Range( _
"A1:L1"), Unique:=False
Sheets("Liste").Select
'Tri de la feuille Liste, par Option 4 puis 5
Cells.Select
Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Key2:=Range("H2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
'Déclaration des variables
Range("A1").Select
Dim wb1 As Workbook
Dim ws3 As Worksheet, ws4 As Worksheet
Dim PTCache1 As PivotCache
Dim pt1 As PivotTable
Dim rngPT1 As Range
'Optimisation (Gel Affichage)
Application.ScreenUpdating = False
'Initialisation des variables
Set wb1 = ActiveWorkbook
Set ws3 = wb1.Worksheets("Liste")
Set rngPT1 = ws3.Cells(1).CurrentRegion 'Données sources du TCD
Set ws4 = wb1.Worksheets("Tableau")
'Suppression TCD
ws4.PivotTables(1).TableRange2.Clear
'Création du cache de TCD (à partir de rngPT)
Set PTCache1 = wb1.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=rngPT1)
'Création du TCD en feuille 'Tableau' nommé TCD_1
Set pt1 = PTCache1.CreatePivotTable _
(tabledestination:=ws4.Cells(6, 2), _
TableName:="TCD_1", _
defaultversion:=xlPivotTableVersion10)
With pt1
'Calcul TCD manuel (Optimisation)
.ManualUpdate = True
'Ajout des étiquettes de lignes et colonnes
.AddFields RowFields:=Array("OPTION 4", "OPTION ECO"), _
ColumnFields:="SEXE"
'Ajout champ valeurs
With .PivotFields("NOM")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Caption = "NB NOMS"
End With
'Calcul automatique (affiche le TCD)
.ManualUpdate = False
End With
wb1.ShowPivotTableFieldList = False
With ws4
.Activate
.[A1].Select
End With
Set rngPT1 = Nothing
Set pt1 = Nothing
Set PTCache1 = Nothing
Set ws4 = Nothing: Set ws3 = Nothing
Set wb1 = Nothing
Sheets("Tableau").Activate
'Définition des critères pour SES
Sheets("Génération").Activate
Else: [B1] = 4 And ([C1] = 1 Or [C1] = 2) And ([D1] = 1 Or [D1] = 2) And ([E1] = 1 Or [E1] = 2) And ([K1] = 1 Or [K1] = 2) And ([L1] = 1 Or [L1] = 2)
'Suppression TCD
Sheets("Tableau").Activate
Range("A1:Q300").Select
Range("Q300").Activate
Selection.Delete
'Suppresion Liste
Sheets("Liste").Activate
Range("A1:L400").Select
Range("L400").Activate
Selection.Delete
'Filtre élaboré pour trier la BD élève
Sheets("BD Eleves").Range("A1:L400").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Génération").Range("T17:AE18"), CopyToRange:=Sheets("Liste").Range( _
"A1:L1"), Unique:=False
Sheets("Liste").Select
'Tri de la feuille Liste, par Option 4 puis 5
Cells.Select
Selection.Sort Key1:=Range("H2"), Order1:=xlAscending, Key2:=Range("I2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
'Déclaration des variables
Range("A1").Select
Dim wb2 As Workbook
Dim ws5 As Worksheet, ws6 As Worksheet
Dim PTCache2 As PivotCache
Dim pt2 As PivotTable
Dim rngPT2 As Range
'Optimisation (Gel Affichage)
Application.ScreenUpdating = False
'Initialisation des variables
Set wb2 = ActiveWorkbook
Set ws5 = wb2.Worksheets("Liste")
Set rngPT2 = ws5.Cells(1).CurrentRegion 'Données sources du TCD
Set ws6 = wb2.Worksheets("Tableau")
'Suppression TCD
ws6.PivotTables(1).TableRange2.Clear
'Création du cache de TCD (à partir de rngPT)
Set PTCache2 = wb2.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=rngPT2)
'Création du TCD en feuille 'Tableau' nommé TCD_1
Set pt2 = PTCache2.CreatePivotTable _
(tabledestination:=ws6.Cells(6, 2), _
TableName:="TCD_1", _
defaultversion:=xlPivotTableVersion10)
With pt2
'Calcul TCD manuel (Optimisation)
.ManualUpdate = True
'Ajout des étiquettes de lignes et colonnes
.AddFields RowFields:="OPTION ECO", _
ColumnFields:=Array("OPTION 4", "SEXE")
'Ajout champ valeurs
With .PivotFields("NOM")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Caption = "NB NOMS"
End With
'Calcul automatique (affiche le TCD)
.ManualUpdate = False
End With
wb2.ShowPivotTableFieldList = False
With ws6
.Activate
.[A1].Select
End With
Set rngPT2 = Nothing
Set pt2 = Nothing
Set PTCache2 = Nothing
Set ws6 = Nothing: Set ws5 = Nothing
Set wb2 = Nothing
Sheets("Tableau").Activate
End If
End Sub |