Excel VBA数据透视表使用变量计算字段创建

时间:2017-06-07 17:19:42

标签: excel-vba pivot-table calculated-field vba excel

我环顾四周,并没有找到这类问题的答案。我做错了可能是微不足道的事情,但我已经尝试了几种不同的格式而没有成功。我有一个很长的VBA脚本,它使用计算来确定要包含在数据透视表中的字段。然后我将返回的变量字段转换为变量(pivot1,pivot2等)。我无法在添加计算字段时专门命名字段,因为我不知道它们将是什么。第二段代码抛出错误"无法获取数据透视表类的#PivotFields属性"。对此有任何帮助将非常感激。

ActiveSheet.PivotTables("Pivot Table 1").CalculatedFields.Add "Total", "= pivot1 _
    & " + " & pivot2 & " + " & pivot3 & " + " & pivot4 & " + " & pivot5 & " + " & pivot6 _
    & " + " & pivot7 & " + " & pivot8 & " + " & pivot9 & " + " & pivot10 & " + " & pivot11 _
    & " + "& pivot12", True
ActiveSheet.PivotTables("Pivot Table 1").AddDataField ActiveSheet.PivotTables _
    ("Pivot Table 1").PivotFields("Total"), "Grand Total", xlSum

0 个答案:

没有答案
相关问题