2017-10-18 6 views
0

피벗 테이블을 만들었으며 피벗 테이블의 다른 두 열 사이의 차이 (값)를 인쇄 할 열을 추가하려고합니다. 필자는 피벗 테이블 작성과 관련한 제 코드 부분을 제공 할 것입니다. 그것이 분명하기 때문에, 나는 그것의 마지막 부분에서 무언가를 시도했지만, 아무것도 인쇄하지 않을 것이다. 그것은 실행되지만 아무것도 인쇄하지 않습니다. 피벗 테이블이 올바르게 인쇄되지만 새 열은 인쇄되지 않습니다.피벗 테이블에 계산 된 필드 추가

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

체크 https://stackoverflow.com/a/26970322/7889129 – Maddy

+0

매크로 레코더가 제작 한 것이보고하고 적응하려고 했습니까? – QHarr

답변

2

계산 된 필드를 추가 한 후 피벗 테이블에 표시되도록 xlDataField로 방향을 설정해야합니다.

이런 식으로 뭔가를 시도 ...

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

위대한 작품이지만, 실제로 원하는 결과를 인쇄하지 않습니다. 나는 열의 수식과이 새로운 수식을 수정하려고 시도했습니다. .CalculatedFields.Add "Diff", "= GETPIVOTDATA GETPIVOTDATA ('Monthly $/SU Actual', $ A $ 15, 'T-Lane', 'Athens to GREECE') ('Monthly $/SU 예측', $ A $ 15, 'T-Lane' ') " .PivotFields ("Diff ") Orientation = xlDataField –

+0

그러나이 새로운 수식에는 속성을 설정할 수 없습니다.라는 메시지가 표시됩니다. –

+0

계산 된 필드를 수동으로 만들고 해당 코드를 분석 할 때 매크로를 기록하십시오. 당신은 아이디어를 얻은 다음 요구 사항에 따라 조정할 수 있습니다. – sktneer