2017-10-18 10 views
0

J'ai créé un tableau croisé dynamique et je souhaite y ajouter une colonne, dans laquelle il imprime la différence (en valeur) entre deux autres colonnes du tableau croisé dynamique. Je fournirai la partie de mon code relative à la construction du tableau croisé dynamique. Comme il est clair, j'ai essayé quelque chose dans la dernière partie, mais il n'imprime rien. Ça marche mais ça n'imprime rien. Il imprime uniquement le tableau croisé dynamique, mais pas la nouvelle colonne.Ajouter un champ calculé dans le tableau croisé dynamique

Dim DSheet As Worksheet 
Dim PCache As PivotCache 
Dim PTable As PivotTable 
Dim PRange As Range 

Application.DisplayAlerts = True 
Set DSheet = Worksheets("Budget_Report") 

Set PRange = DSheet.Range(Cells(1, 27), Cells.SpecialCells(xlCellTypeLastCell)) 

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) 
Set PTable = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(15, 1), TableName:="PivotTable1") 

With ActiveSheet.PivotTables("PivotTable1") 
With .PivotFields("T-Lane") 
    .Orientation = xlRowField 
    .Position = 1 
    .Subtotals(1) = True 
    .Subtotals(1) = False 
End With 

With .PivotFields("Monthly Cost FCST") 
    .Orientation = xlDataField 
    .Position = 1 
    .Function = xlAverage 
    .NumberFormat = "0.00" 
    .Caption = "Monthly Cost Forecast" 
End With 

With .PivotFields("Monthly Vol FCST") 
    .Orientation = xlDataField 
    .Position = 2 
    .Function = xlAverage 
    .NumberFormat = "0.00" 
    .Caption = "Monthly Vol Forecast" 
End With 

With .PivotFields("Monthly $/SU FCST") 
    .Orientation = xlDataField 
    .Position = 3 
    .Function = xlAverage 
    .NumberFormat = "0.00" 
    .Caption = "Monthly $/SU Forecast" 
End With 

With .PivotFields("Monthly Cost Actuals") 
    .Orientation = xlDataField 
    .Position = 4 
    .Function = xlSum 
    .NumberFormat = "0.00" 
    .Caption = "Monthly Cost Actual" 
End With 

With .PivotFields("Monthly Vol Actuals") 
    .Orientation = xlDataField 
    .Position = 5 
    .Function = xlSum 
    .NumberFormat = "0.00" 
    .Caption = "Monthly Vol Actual" 
End With 

With .PivotFields("Monthly $/SU Actuals") 
    .Orientation = xlDataField 
    .Position = 6 
    .Function = xlAverage 
    .NumberFormat = "0.00" 
    .Caption = "Monthly $/SU Actual" 
End With 

    .CalculatedFields.Add "Diff", "= 'Monthly Cost FCST'- 'Monthly Cost Actuals'" 

End With 
+0

check https://stackoverflow.com/a/26970322/7889129 – Maddy

+0

Avez-vous essayé de voir ce que l'enregistreur de macro produit et l'adaptation? – QHarr

Répondre

2

Après avoir ajouté le champ calculé, vous devez définir son orientation xlDataField pour le rendre visible sur le tableau croisé dynamique.

Essayez quelque chose comme ça ...

.CalculatedFields.Add "Diff", "= 'Monthly Cost FCST'- 'Monthly Cost Actuals'" 
.PivotFields("Diff").Orientation = xlDataField 
+0

Grand cela a fonctionné, mais en réalité il n'imprime pas les résultats que je veux .. J'ai essayé de fixer la formule de la colonne et la nouvelle formule qui fonctionne est celle-ci .. .CalculatedFields.Add "Diff", "= GETPIVOTDATA ('Prévision mensuelle $/SU', A $ 15, 'T-Lane', 'Athènes vers la Grèce') - GETPIVOTDATA ('Mensuel $/SU Actuel', $ A $ 15, 'T-Lane', 'Athènes vers la GRÈCE ' .PivotFields ("Diff"). Orientation = xlDataField –

+0

Cependant, cette nouvelle formule a une erreur car elle indique Impossible de définir la propriété –

+0

Enregistrez la macro pendant que vous créez manuellement le champ calculé, puis analysez le code, ce qui donnera vous une idée, puis modifiez-le selon vos besoins. – sktneer