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
| Sub x()
Application.ScreenUpdating = False 'Evite la mise à jour de l'écran
Application.DisplayAlerts = False 'Evite les message pour valider la fusion.
n = Range("B" & Rows.Count).End(xlUp).Row 'On compte le nombre de ligne de notre plage
For Each cell In Range(Cells(n + 1, 1), Cells(n + 1, 2)) 'Je pense qu'il y a mieux, mais à cause d'une erreur que j'ai remarquée, j'ai du rajouter cette ligne
cell.Value = "X"
Next
'Première étape, on fusionne la colonne C en prenant en compte la colonne A et B
For Each cell In Range("C3:C" & n)
For i = 1 To n - cell.Row
If cell.Offset(0, -1).Value = cell.Offset(1, -1).Value And cell.Offset(0, -2).Value = cell.Offset(1, -2).Value Then
If cell.Value = cell.Offset(1, 0).Value Then
With Range(cell, cell.Offset(1, 0))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else: Exit For
End If
Else: Exit For
End If
Next
Next
Cells(n + 1, 1).EntireRow.Delete
'Deuxième étape on commence par ta colonne B puisque tu ne veux pas tout à fait fusionner les cellules identiques successives mais aussi prendre en compte les données de la colonne A
For Each cell In Range("B3:B" & n)
For i = 1 To n - cell.Row
If cell.Offset(0, -1).Value = cell.Offset(1, -1).Value Then
If cell.Value = cell.Offset(1, 0).Value Then
With Range(cell, cell.Offset(1, 0))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else: Exit For
End If
Else: Exit For
End If
Next
Next
Cells(n + 1, 1).EntireRow.Delete
' Troisième étape on fusionne les cellules successives et identiques de la colonne A
For Each cell In Range(Cells(n + 1, 1), Cells(n + 1, 2))
cell.Value = "X"
Next
For Each cell In Range("A3:A" & n)
For i = 1 To n - cell.Row
If cell.Value = cell.Offset(1, 0).Value Then
With Range(cell, cell.Offset(1, 0))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else: Exit For
End If
Next
Next
Cells(n + 1, 1).EntireRow.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub |
Partager