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
|
Sub DupliquerLesLignesAvecTri()
Dim I As Long, J As Long, LigneDebut As Long, LigneFin As Long, NbPresents As Long
Dim Liste As Range
With ActiveSheet
LigneDebut = 3
LigneFin = .Cells(.Rows.Count, 1).End(xlUp).Row
Set Liste = .Range(.Cells(LigneDebut, 1), .Cells(LigneFin, 1))
For I = 1 To Liste.Count
With Liste(I)
NbPresents = WorksheetFunction.CountIf(Liste, .Value)
Debug.Print Liste(I) & " : " & WorksheetFunction.CountIf(Liste, .Value)
If NbPresents < .Offset(0, 1) Then
For J = 1 To .Offset(0, 1) - NbPresents
.EntireRow.Copy Destination:=ActiveSheet.Cells(LigneFin + 1, 1)
LigneFin = LigneFin + 1
Next J
End If
End With
Next I
' Tri du tableau
TrierUnTableau ActiveSheet, LigneDebut, 1
End With
Set Liste = Nothing
End Sub
Sub TrierUnTableau(ByVal FeuilleATrier As Worksheet, ByVal LigneDeTitre As Long, ByVal ColonneATrier As Long)
Dim DerniereColonne As Long
Dim DerniereLigne As Long
Dim AireATrier As Range
Dim AireColonne As Range
With FeuilleATrier
DerniereColonne = .Cells(LigneDeTitre, .Columns.Count).End(xlToLeft).Column
DerniereLigne = .Cells(.Rows.Count, ColonneATrier).End(xlUp).Row
If DerniereLigne > LigneDeTitre Then
Set AireATrier = .Range(.Cells(LigneDeTitre, 1), .Cells(DerniereLigne, DerniereColonne))
Set AireColonne = .Range(.Cells(LigneDeTitre, ColonneATrier), .Cells(DerniereLigne, ColonneATrier))
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=AireColonne, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange AireATrier
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set AireColonne = Nothing
Set AireATrier = Nothing
End If
End With
End Sub |
Partager