【问题标题】:Pivot table on the same worksheet with the data数据透视表在同一个工作表上
【发布时间】:2018-03-11 06:01:17
【问题描述】:

我创建了各种数据透视表,但仅在其他工作表(插入新工作表)中从数据中创建。现在,我在要创建数据透视表的同一个 Excel 表中拥有数据。它一直在说它在我设置 PCache 的那一行有问题。我在下面提供代码

Sub a()

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

Set PSheet = ActiveSheet

LastRow = PSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = PSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = PSheet.Cells(1, 1).Resize(LastRow, LastCol)

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable TableDestination:=PSheet.Cells(2, 13), TableName:="PivotTable1"

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Destination")
    .Orientation = xlRowField
    .Position = 1
    .Subtotals(1) = True
    .Subtotals(1) = False
End With

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Total trucks")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlSum
    .Name = "Sum of Quantity (cases/sw)"
End With

'PTable.LayoutRowDefault = xlTabularRow
'Range("M2").Value = "Commodity Code"

End Sub

【问题讨论】:

    标签: vba excel pivot-table


    【解决方案1】:

    请替换这些行:

    Set PCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 13), TableName:="PivotTable")
    
    Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")
    

    与:

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable TableDestination:= _
    PSheet.Cells(2, 13), TableName:="PivotTable1"
    

    并将TableName 重命名为PivotTable1

    【讨论】:

    • 我刚刚做到了,不幸的是,同一部分仍然存在问题!我已经上传了更改后的更新代码!
    • 唯一的问题是,即使运行得很好,最后还是会说“无法设置 PivotField 类的 Name 属性...知道为什么吗?
    • 我稍后会检查,因为我正在开会
    【解决方案2】:

    阅读下面的代码并在代码的 cmets 中进行解释:

    Option Explicit
    
    Sub a()
    
    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
    
    Set PSheet = ActiveSheet
    
    With PSheet
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Set PRange = .Cells(1, 1).Resize(LastRow, LastCol)
    End With
    
    ' first: set the Pivot Cache object
    Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange.Address(False, False, xlA1, xlExternal))
    
    ' Second: set the Pivot Table object
    Set PTable = PSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=PSheet.Cells(2, 13), TableName:="PivotTable1")
    
    ' after we set the Pivot-Table object, use the object to modify it's properties
    With PTable
        With .PivotFields("Destination")
            .Orientation = xlRowField
            .Position = 1
            .Subtotals(1) = True
            .Subtotals(1) = False
        End With
    
        With .PivotFields("Total trucks")
            .Orientation = xlDataField
            .Position = 1
            .Function = xlSum
            .Name = "Sum of Quantity (cases/sw)"
        End With
    End With
    
    'PTable.LayoutRowDefault = xlTabularRow
    'Range("M2").Value = "Commodity Code"
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2013-12-23
      • 2017-11-22
      相关资源
      最近更新 更多