用于创建数据透视表的 VBA 代码

标签 vba excel pivot-table

我创建了一个构建数据透视表的宏。它运行得很好,但我想取消提供的屏幕截图中带圆圈的内容.. 我不想让总数在枢轴内。只有在底部的总计!我正在提供我的代码和屏幕截图。此外,是否可以将 B 列中的“行标签”文本替换为“FDD”?

enter image description here

enter image description here

     Sub aa()

 'Declare Variables
    Dim PSheet As Worksheet
    Dim DSheet As Worksheet
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    Dim PRange As Range
    Dim LastRow As Long
    Dim LastCol As Long

    'Insert a New Blank Worksheet
    On Error Resume Next
    Application.DisplayAlerts = False
    Worksheets("Customs_report").Delete
    Sheets.Add Before:=ActiveSheet
    ActiveSheet.Name = "Customs_report"
    Application.DisplayAlerts = True
    Set PSheet = Worksheets("Customs_report")
    Set DSheet = Worksheets("Zeikan")

    'Define Data Range
    LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = DSheet.Cells(6, Columns.Count).End(xlToLeft).Column
    Set PRange = DSheet.Cells(6, 1).Resize(LastRow, LastCol)

    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
    TableName:="PivotTable")

    'Insert Blank Pivot Table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")

    'Insert Row Fields
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Commodity Code")
    .Orientation = xlRowField
    .Position = 1
    .Name = "Commodity Code"
    End With
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Location")
    .Orientation = xlRowField
    .Position = 2
    End With

    'Insert Column Fields
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Quantity (cases/sw)")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlSum
    .Name = "Sum of Quantity (cases/sw)"
    End With
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Quantity (items)")
    .Orientation = xlDataField
    .Position = 2
    .Function = xlSum
    .Name = "Sum of Quantity (items)"
    End With
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Net value")
    .Orientation = xlDataField
    .Position = 3
    .Function = xlSum
    .Name = "Sum of Net value"
    End With
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Gross weight (Kg)")
    .Orientation = xlDataField
    .Position = 4
    .Function = xlSum
    .Name = "Sum of Gross weight (Kg)"
    End With
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Net weight (Kg)")
    .Orientation = xlDataField
    .Position = 5
    .Function = xlSum
    .Name = "Sum of Net weight (Kg)"
    End With


    End Sub

最佳答案

您可以切换小计:

With PTable.PivotFields("Commodity Code")
.Orientation = xlRowField
.Position = 1
.Name = "Commodity Code"
.Subtotals(1) = True
.Subtotals(1) = False
End With

要更改标题,请将表格布局更改为表格
PTable.LayoutRowDefault = xlTabularRow

在添加字段之前。顺便说一句,您应该使用 PTable而不是 ActiveSheet.PivotTables("PivotTable")在您的其余代码中。

关于用于创建数据透视表的 VBA 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46448817/

相关文章:

在循环中选择第一个出现的数字的正则表达式

excel - 保护 Excel 工作表为只读但启用外部数据刷新

python - 使用 xlwings 写入现有 Excel 工作簿

excel - 将矩阵转换为 3 列表 ('reverse pivot' 、 'unpivot' 、 'flatten' 、 'normalize' )

java - Apache POI 数据透视表 : Distinct count (Excel 2013)

vba - 我如何知道一个对象是否已经被引用?

vba - 在 VBA Excel 中复制粘贴范围

mysql - SQL查询询问问题

excel - 找不到 OleDB 数据提供程序 VBA/Excel

java - Apache POI : Excel Pivot Table - Row Label