Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, a As Range, b As Range, c As Range
Dim coul&, cel As Range, i As Variant, j As Variant
Set r = Intersect(Target, [C6:Y10])
If r Is Nothing Then Exit Sub
Application.ScreenUpdating = False
For Each r In r 'en cas d'entrées multiples (copier-coller par exemple)
Set a = Intersect(r.EntireColumn, [11:17])
Set b = Intersect(r.EntireColumn, [20:31])
Set c = Intersect(r.EntireColumn, [35:52])
coul = 16777215 'incolore
'coul = 16751103 'rose
'---cellules défusionnées---
For Each cel In Union(a, b, c)
If cel.MergeCells Then
With cel.MergeArea
.UnMerge
coul = cel.Interior.Color 'mémorisation couleur
.Borders(xlInsideHorizontal).Weight = xlThin 'bordures
End With
Exit For
End If
Next cel
Union(a, b, c) = "" 'RAZ
Union(a, b, c).Interior.ColorIndex = xlNone 'effacement couleur
'---cellules fusionnées---
i = Application.Match(r(9 - r.Row), [A11:A17], 0)
j = Application.Match(r(10 - r.Row), [A11:A17], 0)
If IsNumeric(i) And IsNumeric(j) Then
Range(a(i), a(j)).Merge
a(i) = r(7 - r.Row) & " - " & r(8 - r.Row) & " " & r(11 - r.Row)
a(i).WrapText = True: a(i).Interior.Color = coul: GoTo 1
End If
i = Application.Match(r(9 - r.Row), [A20:A31], 0)
j = Application.Match(r(10 - r.Row), [A20:A31], 0)
If IsNumeric(i) And IsNumeric(j) Then
Range(b(i), b(j)).Merge
b(i) = r(7 - r.Row) & " - " & r(8 - r.Row) & " " & r(11 - r.Row)
b(i).WrapText = True: b(i).Interior.Color = coul: GoTo 1
End If
i = Application.Match(r(9 - r.Row), [A35:A52], 0)
j = Application.Match(r(10 - r.Row), [A35:A52], 0)
If IsNumeric(i) And IsNumeric(j) Then
Range(c(i), c(j)).Merge
c(i) = r(7 - r.Row) & " - " & r(8 - r.Row) & " " & r(11 - r.Row)
c(i).WrapText = True: c(i).Interior.Color = coul
End If
1 Next r
End Sub