【问题标题】:Adding calculated field using VBA使用 VBA 添加计算字段
【发布时间】:2015-01-14 04:42:21
【问题描述】:

我正在尝试将计算字段添加到在 VBA (Excel 2010) 中创建的数据透视表。数据透视表正在工作,一切都出现了——除了完全不存在的计算字段。

我使用的代码如下:

    Sub Create_Pivot_Table_for_chart2()
 Dim wsnew As Worksheet
 Dim objPivotcache As PivotCache
 Dim objPivotTable As PivotTable

'Adding new worksheet
 Set wsnew = Worksheets.Add
 wsnew.Name = "Test5"

'Creating Pivot cache
 Set objPivotcache = ActiveWorkbook.PivotCaches.Create(xlDatabase, "'datasheet'!B1:BX1000")

'Creating Pivot table
 Set objPivotTable = objPivotcache.CreatePivotTable(wsnew.Range("A1"))

'Setting Fields
 With objPivotTable
 'set row field
 With .PivotFields("Prosperator")
 .Orientation = xlRowField
 .Position = 1
 End With

 'set column field
 With .PivotFields("Business Name")
 .Orientation = xlRowField
 .Position = 2
 End With

  'set calculated field
 .CalculatedFields.Add "TOGrowth%", "= ('ITD Average'- 'Pre-ignition T/O')/'Pre-ignition T/O'"

 'set data field
 .AddDataField .PivotFields("Pre-ignition T/O"), "PI T/O", xlSum
 .AddDataField .PivotFields("ITD Average"), "ITD", xlSum


 End With
END SUB

谢谢

【问题讨论】:

    标签: vba excel-2010 pivot-table


    【解决方案1】:

    查看您的代码,您尚未将计算字段作为数据字段添加到数据透视表中。

    您需要在创建字段后添加以下代码行:

    .PivotFields("TOGrowth%").Orientation = xlRowField
    

    【讨论】:

    • Rried 并且 ut 返回错误“无法设置 pivotfield 类的方向属性。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多