如何使用VBA创建通用数据透视表?

3

我有一个使用数据透视表的计算。
我希望创建一个通用的数据透视表,可以在所有需要进行同样计算的时候使用。
我已经尝试过了,但是计算总是被数据透视表阻塞。

    Sheets.Add.Name = "tdc_flux"
    Sheets("flux phf a+1").Select

      ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "flux phf a+1!R1C1:R1048576C16", Version:=6).CreatePivotTable _
        TableDestination:="tdc_flux!R3C1", TableName:="Tableau croisé dynamique3", _
        DefaultVersion:=6

    Sheets("tdc_flux").Select
    Cells(1, 1).Select

    With ActiveSheet.PivotTables("Tableau croisé dynamique3")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("Tableau croisé dynamique3").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("Tableau croisé dynamique3").RepeatAllLabels _
        xlRepeatLabels
    With ActiveSheet.PivotTables("Tableau croisé dynamique3").PivotFields( _
        "Concatner (ref +div)")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Tableau croisé dynamique3").PivotFields( _
        "Type de flux")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("Tableau croisé dynamique3").AddDataField ActiveSheet. _
        PivotTables("Tableau croisé dynamique3").PivotFields("    En DICtrPr"), _
        "Somme de     En DICtrPr", xlSum

   ActiveSheet.PivotTables("Tableau croisé dynamique3").AddDataField ActiveSheet. _
        PivotTables("Tableau croisé dynamique3").PivotFields("    En DICtrPr"), _
        "Somme de     En DICtrPr", xlSum

这是代码中的一个阻塞部分。
"Somme" 意为英语中的 sum,"Tableau croisé dynamque" 是数据透视表的名称。


块是什么意思?有错误信息吗?程序运行似乎只是冻结了吗? - QHarr
@QHarr 是的,在我加入的那段内容之前,程序运行得很好,但是出现了错误信息。当我打开创建的工作表时,只有列和行的名称,没有值。 - XDSSIOP
什么是错误信息? - QHarr
@QHarr 执行错误 1004 无法读取 PivotTable 类的 PivotField 属性。 - XDSSIOP
1个回答

0

我强烈建议使用引用,例如“pc”表示数据透视缓存,“pt”表示数据透视表。

在代码结尾之前,您必须先添加数据字段,如行字段和列字段。

Private Sub GeneralPivot()
    Dim pc As PivotCache
    Dim pt As PivotTable

    Sheets.Add.Name = "tdc_flux"

    Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "flux phf a+1!R1C1:R1048576C16", Version:=6)

    With pc
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault ' xlMissingItemsNone might be better!
    End With

    Set pt = pc.CreatePivotTable( _
        TableDestination:="tdc_flux!R3C1", _
        TableName:="Tableau croisé dynamique3", _
        DefaultVersion:=6)

    With pt
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
        .RepeatAllLabels xlRepeatLabels
    End With

    With pt.PivotFields("Concatner (ref +div)")
        .Orientation = xlRowField
        .Position = 1
    End With

    With pt.PivotFields("Type de flux")
        .Orientation = xlColumnField
        .Position = 1
    End With

    With pt.PivotFields("    En DICtrPr")
        .Orientation = xlDataField
        .Function = xlSum
        .Name = "Somme de En DICtrPr"
    End With

End Sub

为了防止过时的数据,设置MissingItemsLimit = xlMissingItemsNone可能会很有用。
通常不需要选择或激活任何内容

非常感谢,我现在有数据了,但它目前出现了错误。".Name = "Somme de En DICtrPr" - XDSSIOP
最后一个数据字段(数据字段)在您和我的代码中被插入了两次。我删除了那部分。当然,您可以基于相同的数据字段添加另一个数据字段,但是需要使用新名称和其他功能。 - Asger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接