TCD via VBA
C
Bonjour,
Je souhaiterai via un bouton vba faire un tableau croisé dynamique comme en feuille TCD_Automatique via vba et avec la possibilité de rajouter autant d’adhérents que je le souhaite avec actualisation du tcd ...
Merci d'avance à celui ou celle qui prendra du temps pour moi.
j
bonjour
surtout pas de VBA
je suppose que tu recrées ta BD souvent, et qu'à chaque recréation tu veux avoir le TCD.
il suffit de créer ton TCD dans un fichier à part, basé sur le fichier FOE ou FOS (Fichier Origine Exporté ou Fichier Origine Saisi au clavier)
tu crées plusieurs fois par jour le FOE ou FOS, en écrasant l'ancien.
Tu ouvres le fichier contenant le TCD, tu mets à jour.
Simple.
Bonjour,
Une création TCD en VBA, pour le principer.
Cdlt.
Option Explicit
Private Sub cmdCreatePivotTable_Click()
'Déclaration des variables
Dim wb As Workbook
Dim wsData As Worksheet, wsPT As Worksheet
Dim lastCol As Long, lastRow As Long
Dim rngPT As Range
Dim PTCache As PivotCache
Dim PT As PivotTable
'Initialisation des variables
Set wb = ActiveWorkbook
Set wsData = wb.Worksheets("Adhérents")
Set wsPT = wb.Worksheets("TCD")
'Suppression TCD
On Error Resume Next
wsPT.PivotTables(1).TableRange2.Clear
On Error GoTo 0
'Détermination de la plage des données source du TCD
With wsData
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rngPT = .Cells(1).Resize(lastRow, lastCol)
End With
'Création TCD
Set PTCache = wb.PivotCaches.Create _
(SourceType:=xlDatabase, _
SourceData:=rngPT)
Set PT = PTCache.CreatePivotTable _
(tabledestination:=wsPT.Cells(2, 1), _
TableName:="PT_1")
With PT
.ManualUpdate = True
.AddFields RowFields:="Prenom", ColumnFields:="Réglé"
With .PivotFields("Perf")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "#,##0"
.Caption = "NB Perf"
End With
.RowAxisLayout xlTabularRow
.TableStyle2 = "PivotStyleLight8"
.ManualUpdate = False
End With
'RAZ variables
Set PT = Nothing
Set PTCache = Nothing
Set rngPT = Nothing
Set wsPT = Nothing: Set wsData = Nothing
Set wb = Nothing
End Sub