Option Explicit
Sub Compil()
Dim BDComp As Worksheet, x As Integer, TbNameComp As String, tbName As String, C As Integer, H As String, ShName As String
Set BDComp = ThisWorkbook.Sheets("BD_Compil")
TbNameComp = BDComp.Cells(1, 1).ListObject.Name
H = Replace(Join(Application.Transpose(Application.Transpose(BDComp.ListObjects(TbNameComp).HeaderRowRange.Value)), "|"), "Employé|Charge", "")
Application.ScreenUpdating = False
For x = 3 To ThisWorkbook.Sheets.Count
ShName = Sheets(x).Name
tbName = Sheets(x).Range("A17").ListObject.Name
With BDComp.ListObjects(TbNameComp)
If H Like "*" & ShName & "*" Then ' on check si dans le tableau TbNameComp la colonne de l'onglet concerné existe
.ListColumns(ShName).DataBodyRange.Value = Sheets(x).ListObjects(tbName).ListColumns(3).DataBodyRange.Value
Else
.ListColumns.Add ' si la colonne de l'onglet concerné n'existe pas on l'ajoute
C = .ListColumns.Count ' N° de la dernière colonne
.HeaderRowRange(C).Value = ShName ' on nomme l'entête de la nouvelle colonne avec le nom de l'onglet
.ListColumns(ShName).DataBodyRange.Value = Sheets(x).ListObjects(tbName).ListColumns(3).DataBodyRange.Value
End If
End With
Next
Application.ScreenUpdating = True
End Sub