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

我创build了一个构build数据透视表的macros。 它运行的很好,但我想解雇提供的屏幕截图中圈出的内容..我不想有总数内的枢纽。 只有在总计的底部! 我正在提供我的代码和屏幕截图。 此外,是否可以用“FDD”replaceB列中的“行标签”文本?

在这里输入图像描述

在这里输入图像描述

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")在其余的代码。