数据透视字段集合

我试图:

  1. 将PivotTable.PivotFields集合对象复制到临时对象
  2. 对临时的进行一些修改,
  3. 然后将原始PivotTable.PivotFields集合指向新的PivotFields集合。

我试图使用下面的代码。 当我尝试使用“新数据透视字段”时,在指定的行上出现编译错误。 我究竟做错了什么? 任何帮助将appriciated。

Public Sub ChangePivotTable() Dim objPivotTable As PivotTable Dim objPivotFields As PivotFields Dim objTempPivotFields As PivotFields 'Get Pivot Table from worksheet Set objPivotTable = Worksheets("PivotTableWorksheet").PivotTables(1) 'Set reference to PivotFields Collection of PivotTable Set objPivotFields = objPivotTable.PivotFields 'Compile error: Invalid use of New Keyword Set objTempPivotFields = New PivotFields 'Pass Collection by Value Let objTempPivotFields = objPivotFields 'Make changes to Temporary PivotFields Collection Dim intIndex As Integer For intIndex = 6 To 2156 objTempPivotFields(intIndex).Orientation = xlDataField objTempPivotFields(intIndex).Function = xlAverage Next intIndex 'Set original PivotFields Collection to temporary one Set objPivotFields = objTempPivotFields End Sub