【问题标题】:VBA code for creating Pivot Table用于创建数据透视表的 VBA 代码
【发布时间】:2017-09-27 13:11:39
【问题描述】:

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

     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

【问题讨论】:

    标签: vba excel pivot-table


    【解决方案1】:

    您可以切换小计:

    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")

    【讨论】:

    • 完美!我会在 5 分钟内接受答案,因为它不允许我!问题的第二部分怎么样?有什么解决办法吗?
    • 最后一个问题!我更新了我的 excel 的图片以向您展示问题。最后一行的行值是空白...为什么会发生这种情况,我该如何解决?
    • @PericlesFaliagas 您的来源范围太大。您应该使用Set PRange = DSheet.Cells(6, 1).Resize(LastRow - 5, LastCol),因为它从第 6 行开始。
    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多