Sub mTest()
Créer_Exemple
Créer_TCD
End Sub
Private Sub Créer_Exemple()
formules = Array("=""NOM""&ROW()-1", "=""PRE""&RC[-1]", "=RANDBETWEEN(22000,35000)", "=CHAR(RANDBETWEEN(65,67))")
[A1:D1] = Array("Nom", "Prénom", "Code Postal", "Postes")
[A2:D2].Formula = formules: [A2:D30].FillDown: [A1:D30] = [A1:D30].Value
End Sub
Private Sub Créer_TCD()
Dim Source$, Desti$, TCD As PivotTable
Source = "Feuil1!R1C1:R30C4": Desti = "Feuil1!R3C7"
Set TCD = ActiveWorkbook.PivotCaches.Create(1, Source).CreatePivotTable(TableDestination:=Desti, TableName:=" ")
With TCD.PivotFields("Postes"): .Orientation = 1: .Position = 1: End With
TCD.AddDataField TCD.PivotFields("Nom"), "Nombre de Nom", xlCount
TCD.CompactLayoutRowHeader = "Postes différents"
TCD.DataPivotField.PivotItems("Nombre de Nom").Caption = "Nombre de postes"
End Sub