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
|
Sub TestCinqMeilleuresCriticites()
CinqMeilleuresCriticites Sheets("Feuil1"), 39, 7
End Sub
Sub CinqMeilleuresCriticites(ByVal FeuilleATrier As Worksheet, ByVal LigneDeTitre As Long, ByVal LigneDeDestination As Long)
Dim DerniereLigne As Long
Dim DerniereColonne As Long
Dim AireTableau As Range
Dim AireIdentifiant As Range
Dim AireCriticite As Range
Dim AireProbabilite As Range
Dim AireMontant As Range
With FeuilleATrier
DerniereLigne = .Cells(.Rows.Count, 2).End(xlUp).Row
DerniereColonne = .Cells(LigneDeTitre, .Columns.Count).End(xlToLeft).Column
Set AireTableau = .Range(.Cells(LigneDeTitre, 2), .Cells(DerniereLigne, DerniereColonne))
Set AireIdentifiant = .Range(.Cells(LigneDeTitre, 2), .Cells(DerniereLigne, 2))
Set AireCriticite = .Range(.Cells(LigneDeTitre, 5), .Cells(DerniereLigne, 5))
Set AireMontant = .Range(.Cells(LigneDeTitre, 6), .Cells(DerniereLigne, 6))
Set AireProbabilite = .Range(.Cells(LigneDeTitre, 7), .Cells(DerniereLigne, 7))
' Tri par ordre décroissant des champs Criticité, Probabilité, Montant
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=AireCriticite, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=AireProbabilite, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=AireMontant, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With .Sort
.SetRange AireTableau
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Copie des 5 premières lignes du tableau trié
If AireTableau.Rows.Count > 5 Then
AireTableau.Rows("2:6").Copy Destination:=.Cells(LigneDeDestination, 2)
End If
' Tri par ordre croissant des identifiants
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=AireIdentifiant, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange AireTableau
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set AireTableau = Nothing
Set AireIdentifiant = Nothing
Set AireCriticite = Nothing
Set AireMontant = Nothing
Set AireProbabilite = Nothing
End With
End Sub |
Partager