AddDataField从"报告过滤器"中删除字段。

时间:2016-08-02 20:11:46

标签: excel vba excel-vba macros excel-2010

为什么数据透视表的AddDataField()方法会删除"报告过滤器"?我使用GUI创建了一个新的数据透视表并记录了一个宏。一切顺利,除了宏本身不起作用。如何在"报告过滤器"中添加字段?和VBA值(我只能用GUI做到这一点)?

这是我录制的宏。您可以在A1处于活动状态的空白工作表上测试运行此项。最后一个命令从"报告过滤器"中删除该字段,即使在记录宏和使用GUI时一切正常。这真的打败了我。

Option Explicit

Sub Macro8()
'
' Macro8 Macro
'

'
    ActiveCell.FormulaR1C1 = "foo"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    Selection.End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R2C1", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet1!R1C3", TableName:="PivotTable2", DefaultVersion _
        :=xlPivotTableVersion14
    Sheets("Sheet1").Select
    Cells(1, 3).Select
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("foo")
        .Orientation = xlPageField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields("foo"), "Sum of foo", xlSum
End Sub

3 个答案:

答案 0 :(得分:1)

我不明白为什么AddDataField()从"报告过滤器"中删除了该字段,但是我发现这里有几个解决方法:

  1. 使用GUI添加字段
  2. 将字段添加到"值" 第一次,然后只有#34;报告过滤器":

    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields("foo"), "Sum of foo", xlSum
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("foo")
        .Orientation = xlPageField
        .Position = 1
    End With
    
  3. 使用此字段将字段读入"报告过滤器"在它被AddDataField()删除后。

    Sub addDataField(table As Variant, field As Variant, name As Variant, fieldFunction As Variant)
        Dim orientation As Variant
        orientation = field.orientation
        table.addDataField field, name, fieldFunction
        field.orientation = orientation
    End Sub
    

    因此,示例的最后一个命令应该更改为:

    'ActiveSheet.PivotTables("PivotTable2").addDataField ActiveSheet.PivotTables( _
    '    "PivotTable2").PivotFields("foo"), "Sum of foo", xlSum
    addDataField ActiveSheet.PivotTables("PivotTable2"), ActiveSheet.PivotTables("PivotTable2").PivotFields("foo"), "Sum of foo", xlSum
    

答案 1 :(得分:0)

编辑:

我错了。在单元格A1类型的Sheet2上打开一个新工作簿"标签"在B1类型"值"。然后,我用" a"," b"填充前50行A列。或" c",统称我的标签。在标签旁边,我做了一个RandBetween(1,50)。下面的代码将在Sheet1的单元格A1中输出数据透视表。我建议玩这个来了解如何添加不同的字段。

Option Explicit

Sub pt()
Dim pc As PivotCache
Dim pt As PivotTable
Dim wb As Workbook

Set wb = ThisWorkbook

With wb
    Set pc = .PivotCaches.Create(xlDatabase,     .Sheets("Sheet2").Range("a2").CurrentRegion)
End With

With pc
    Set pt = .CreatePivotTable(ThisWorkbook.Sheets("Sheet1").Range("a1"))
End With

With pt
    .AddDataField .PivotFields("label")
    .AddDataField .PivotFields("value"),,xlSum
    .AddFields RowFields:="label", PageFields:="value"
End With


End Sub

答案 2 :(得分:0)

@jumxozizi,您还需要在第三个解决方法中设置.Position

Sub addDataField(pv, ByVal field, Optional func, Optional caption)
    'Adds a data field to a PivotTable report preserving field's .Orientation and .Position
    'pv - PivotTable object, field - source field name
    'func - the function performed in the added data field
    'caption - the label used in the PivotTable report to identify this data field
    Set field = pv.PivotFields(field)
    orient = field.Orientation
    pos = field.Position
    pv.addDataField field, caption, func
    If orient = xlHidden Or field.Orientation <> xlHidden Then Exit Sub
    Dim lower_fields() As String
    ReDim Preserve lower_fields(pos To pos)
    lower_fields(pos) = field.Name
    'Temporarly hide group fields
    For Each f In pv.PivotFields
        If f.Orientation = orient Then
            f_pos = f.Position + 1 '-1 position after the `field` was hidden
            If f_pos > pos Then
                If UBound(lower_fields) < f_pos Then _
                    ReDim Preserve lower_fields(pos To f_pos)
                lower_fields(f_pos) = f.Name
                f.Orientation = xlHidden
            End If
        End If
    Next f
    'Add group fields back in right order
    For Each f_name In lower_fields
        pv.PivotFields(f_name).Orientation = orient
    Next f_name
End Sub

但是因为您首先设置了.Orientation,所以默认的.Position是最后一个。此行为可能导致数据透视表与工作表上的其他内容重叠。因此,在上述功能中,我临时删除了field下的分组字段。然后按正确的顺序将它们全部重新添加。