1

我试着:

  1. 将 PivotTable.PivotFields 集合对象复制到一个临时对象
  2. 对临时的进行一些更改,
  3. 然后将原来的 PivotTable.PivotFields Collection 设置为指向新的 PivotFields Collection。

我正在尝试使用以下代码。当我尝试使用“New PivotFields”时,在指示的行上出现编译错误。我究竟做错了什么?任何帮助都将不胜感激。

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
4

0 回答 0