gpt4 book ai didi

vba - 使用 VBA 添加计算字段

转载 作者:行者123 更新时间:2023-12-04 14:00:33 27 4
gpt4 key购买 nike

我正在尝试将计算字段添加到在 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

谢谢

最佳答案

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

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

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

关于vba - 使用 VBA 添加计算字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26969830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com