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
| Option Explicit
Sub Fusion()
Dim f1 As Worksheet, f2 As Worksheet
Dim DerLig As Long, DerCol As Long
Dim i As Long, j As Long
Set f1 = Sheets("BDD")
Set f2 = Sheets("résultats")
Application.ScreenUpdating = False
f1.Select
DerLig = f1.Range("A" & Rows.Count).End(xlUp).Row
DerCol = f1.Range("XFD1").End(xlToLeft).Column
f2.Cells.Clear
'Récupération des entêtes de lignes
f1.Range(Cells(1, 1), Cells(DerLig, DerCol)).Copy f2.Range("A1")
'Insertion d'une colonne pour récupérer le N° des couleurs
f2.Columns("B:B").Insert Shift:=xlToRight
f2.Range("B2:B" & DerLig).Formula = "=Couleur(RC)"
'Tri par référence et par couleur
f2.Sort.SortFields.Clear
f2.Sort.SortFields.Add Key:=f2.Range("A1:A" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
f2.Sort.SortFields.Add Key:=f2.Range("B1:B" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With f2.Sort
.SetRange Range("A1:F" & DerLig)
.Header = xlYes
.SortMethod = xlPinYin
.Apply
End With
'Fusion
For i = DerLig - 1 To 2 Step -1
If f2.Cells(i + 1, "A") = f2.Cells(i, "A") And f2.Cells(i + 1, "B") = f2.Cells(i, "B") Then
For j = 3 To DerCol
If f2.Cells(i + 1, j) <> "" Then
f2.Cells(i, j) = Cells(i, j)
f2.Cells(i, j).EntireRow.Delete
Exit For
End If
Next j
End If
Next i
'On supprime la colonne des N° de couleurs
f2.Columns("B:B").Delete Shift:=xlToLeft
f2.Select
Set f1 = Nothing
Set f2 = Nothing
End Sub
Function Couleur(Cel As Range)
Couleur = Cel.Interior.Color
End Function |
Partager