Private Sub Worksheet_Change(ByVal Target As Range)
Dim tablo As Range, premcol%, dercol%
Application.EnableEvents = False 'désactive les évènements
If ListObjects.Count = 0 Then GoTo 1 'il faut au moins un tableau structuré
Set tablo = ListObjects(1).Range
If Target.Count = 1 Then If Not Intersect(Target, tablo.Rows(1)) Is Nothing Then GoTo 2 'si modification des en-têtes
On Error Resume Next
premcol = tablo.Find("=", , xlFormulas, xlPart, xlByColumns, xlNext).Column - tablo.Column + 1
dercol = tablo.Find("=", , xlFormulas, xlPart, xlByColumns, xlPrevious).Column - tablo.Column + 1
If Err Then If tablo.ListObject.DataBodyRange Is Nothing Then _
tablo(2, 1) = " ": tablo(2, 1) = "": GoTo 2 Else GoTo 1 'si toutes les formules sont supprimées
On Error GoTo 0
If dercol - premcol <> 2 Then GoTo 1
If Intersect(Target, tablo) Is Nothing Then GoTo 2 ' si le tableau n'est pas modifié
With tablo.Cells(2, premcol).Resize(tablo.Rows.Count - 1, 3)
If Application.CountIf(.Columns(1), "#REF!") Then GoTo 1 'si suppression de la colonne Option
If Intersect(Target, .Cells) Is Nothing Then
If .Precedents.Address <> .Columns(0).Address Then GoTo 1 'teste la position des antécédents pour annuler l'insertion de colonnes
Else
.Columns(1) = "=REPT(""" & ChrW(10004) & """,OR(RC[-1]=""BB"",RC[-1]=""DP"",RC[-1]=""PC""))"
.Columns(2) = "=REPT(""" & ChrW(10004) & """,RC[-2]=""PC"")"
.Columns(3) = "=REPT(""" & ChrW(10004) & """,OR(RC[-3]=""PC"",RC[-3]=""DP""))"
End If
End With
GoTo 2
1 Application.Undo 'annule la modification
2 Application.EnableEvents = True 'réactive les évènements
With UsedRange: End With 'actualise
End Sub