Excel 如何用VBA制作通用透视表?

Excel 如何用VBA制作通用透视表?,excel,vba,pivot-table,Excel,Vba,Pivot Table,我使用数据透视表进行了计算。 我想制作一个通用的数据透视表,它将在任何时候都有效,我有同样的计算要做。 我已经试过了,但是不起作用。计算总是在数据透视表中阻塞 Sheets.Add.Name = "tdc_flux" Sheets("flux phf a+1").Select ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "flux phf a+1!R

我使用数据透视表进行了计算。
我想制作一个通用的数据透视表,它将在任何时候都有效,我有同样的计算要做。
我已经试过了,但是不起作用。计算总是在数据透视表中阻塞

    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”是透视表的名称。

我强烈建议使用参考资料,例如。G数据透视缓存为“pc”,数据透视表为“pt”

    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

在代码的末尾,您必须在前面添加数据字段,如rowfield和columnfield

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
可能很有用


通常不需要。

你所说的块是什么意思?是否有错误消息?是的,这个程序运行得很好,直到我输入的段落出现错误消息。当我打开创建的工作表时,只有列和行的名称,但没有值。错误消息是什么?@QHarr Execution error 1004无法读取数据透视表类的数据透视字段属性。非常感谢,我现在有数据,但目前正在安装错误。“.name=“Somme de En DICtrPr”刚刚编辑:最后一个数据透视字段(数据字段)在您和我的代码中插入了两次。我删除了那部分。当然,您可以基于同一数据透视字段添加另一个数据字段,但随后使用新名称和另一个函数。