Macro pour création TCD

Bonjour,

J'ai une trouvé une macro permettant la création d''un TCD :

Sub createPivotTable()

'declare variables to hold row and column numbers that define source data cell range
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long

'declare variables to hold source and destination cell range address
    Dim mySourceData As String
    Dim myDestinationRange As String

'declare object variables th hold references to source ans destination worksheets, and new Pivot Table
    Dim mySourceWorksheet As Worksheet
    Dim myDestinationWorksheet As Worksheet
    Dim myPivotTable As PivotTable

'identify source and destination worksheets
    With ThisWorkbook
        Set mySourceWorksheet = .Worksheets("Productivity Datas")
        Set myDestinationWorksheet = .Worksheets("Hours Booked")
    End With

'obtain address of destination cell range
    myDestinationRange = myDestinationWorksheet.Range("A4").Address(ReferenceStyle:=xlR1C1)

'identify row and column numbers that define source data celle range
    myFirstRow = 1
    myLastRow = 1000
    myFirstColumn = 1
    myLastColumn = 36

'obtain address of source data cell range
    With mySourceWorksheet.Cells
        mySourceData = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn)).Address(ReferenceStyle:=xlR1C1)
    End With

'create Pivot Table cache and create Pivot Table report based on that cache
    Set myPivotTable = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=mySourceWorksheet.Name & "!" & mySourceData).createPivotTable(TableDestination:=myDestinationWorksheet.Name & "!" & myDestinationRange, TableName:="HoursBooked")

'add, organize and format Pivot table fields
    With myPivotTable
        .PivotFields ("Name of employee or applicant")
        .Orientation = xlRowField
        With .PivotFields("Number (unit)")
             .Orientation = xlDataField
             .Position = 1
             .Function = xlSum
        End With
    End With
End Sub

J'ai une erreur sur Set myPivotTable = ... mais je n'arrive pas à savoir d'où elle vient ..

Merci à ceux qui pourront m'aider...

Bonjour,

Merci de jondre un classeut et nous préciser le résultat escompté !...

Cdlt.

Le résultat voulu c'est la création d'un TCD comme sur la page "Hours Booked" mais en ayant pour coin haut gauche A4...

6azkaet.xlsm (177.42 Ko)

bonjour

salut Jean-Eric

on ne recrée pas en permanence un TCD

par définition du TCD, on le crée au clavier/souris une fois au départ, ensuite on le met à jour d'un clic (100 fois par jour si on veut)

il semble donc que tu doives replanifier le fonctionnement du TCD avec les données (selon comment celles-ci te sont fournies).

j'ai fait des centaines de TCD, jamais de VBA, jamais de "recréation" de TCD.

maintenant, je fais moins de TCD, car Power BI Desktop gratuit est tellement pareil en 100000000 fois mieux

Oui, j'ai eu la même réflexion, et mon code est terminé. Merci.

Rechercher des sujets similaires à "macro creation tcd"