【问题标题】:VBA Pivot TableVBA 数据透视表
【发布时间】:2017-04-06 10:48:49
【问题描述】:

我正在尝试编写一些 VBA,它将在新选项卡中构建数据透视表。当我运行代码时,在创建数据透视表缓存时出现类型不匹配错误。

代码如下,任何帮助将不胜感激......希望重新审视它可以发现我缺少的东西

Sub makeAPivotTable()

    Dim sSheet, sSheet2 As Worksheet 'sSheet is where the data is, sSheet2 is where the pivot will be built
    Dim cCache As PivotCache
    Dim ptTable As PivotTable
    Dim rRange As Range
    Dim rLastRow, cLastColumn As Long

    ' Insert the new sheet for the pivot table to reside
    Application.DisplayAlerts = False
    Sheets.Add Before:=ActiveSheet
    ActiveSheet.name = "PivotTable"
    Application.DisplayAlerts = True
    Set sSheet2 = Worksheets("PivotTable")
    Set sSheet = Worksheets("Interactions db")

    ' define the range (the data that you want to put into the pivot table
    rLastRow = sSheet.Cells(Rows.Count, 1).End(xlUp).Row
    cLastColumn = sSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Set rRange = sSheet.Cells(1, 1).Resize(rLastRow, cLastColumn)

    ' create the cache for the pivot table
    Set cCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=rRange). _
    CreatePivotTable(TableDestination:=sSheet2.Cells(2, 2), _
    TableName:="SalesPivotTable")


    ' insert the blank table
    Set ptTable = cCache.CreatePivotTable _
        (TableDestination:=sSheet2.Cells(1, 1), TableName:="SalesPivotTable")


    'Insert Row Fields
    With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("customer")
    .Orientation = xlRowField
    .Position = 1
    End With


    'Insert Column Fields
    'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Interaction Type")
    '.Orientation = xlColumnField
    '.Position = 1
    'End With

    'Insert Data Field
    With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("interactionType")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlSum
    .NumberFormat = "#,##0"
    .name = "Person "
    End With


    ' do some formatting






End Sub

【问题讨论】:

    标签: vba excel pivot-table type-mismatch


    【解决方案1】:

    试试下面的代码(代码内解释为cmets)

    Option Explicit
    
    Sub makeAPivotTable()
    
        Dim sSheet As Worksheet, sSheet2 As Worksheet  ' sSheet is where the data is, sSheet2 is where the pivot will be built
        Dim cCache As PivotCache
        Dim ptTable As PivotTable
        Dim rRange As Range
        Dim rLastRow As Long, cLastColumn As Long ' need to define each one as Long, otherwise the first one is Variant
    
        ' Insert the new sheet for the pivot table to reside
        Application.DisplayAlerts = False
    
        ' 2 lines below are shorter version to create a new sheet, 
        ' assign it to sSheet2 , and name it "PivotTable"
        On Error Resume Next
        Set sSheet2 = Sheets("PivotTable") '<-- try to set the Sheet (already created in past code runs)
        On Error GoTo 0
    
        If sSheet2 Is Nothing Then '<-- sheet does not exist yet >> Add it
            Set sSheet2 = Sheets.Add(Before:=ActiveSheet)
            sSheet2.Name = "PivotTable"
        End If
    
        Application.DisplayAlerts = True
    
        Set sSheet = Worksheets("Interactions db")
    
        ' define the range (the data that you want to put into the pivot table
        With sSheet
            rLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            cLastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
            Set rRange = .Cells(1, 1).Resize(rLastRow, cLastColumn)
        End With
    
        ' create the cache for the pivot table ***** THIS is the CORRECT Syntax
        Set cCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rRange)
    
        ' CREATE a NEW Pivot Table in sSheet2 sheet
        Set ptTable = sSheet2.PivotTables.Add(PivotCache:=cCache, TableDestination:=sSheet2.Range("A1"), TableName:="SalesPivotTable")
    
        With ptTable
            'Insert Row Fields
            With .PivotFields("customer")
                .Orientation = xlRowField
                .Position = 1
            End With
    
            'Insert Column Fields
            'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Interaction Type")
            '.Orientation = xlColumnField
            '.Position = 1
            'End With
    
            'Insert Data Field
            With .PivotFields("interactionType")
                .Orientation = xlDataField
                .Position = 1
                .Function = xlSum
                .NumberFormat = "#,##0"
                .Name = "Person "
            End With
    
            ' do some formatting
        End With
    
    End Sub
    

    【讨论】:

    • 如果我没记错的话应该是这样 - Set ptTable = sSheet2.PivotTables.Add(PivotCache:=cCache, TableDestination:=sSheet2.Range("A1"), TableName:="SalesPivotTable ")
    • 在开头删除 sSheet2 也很重要,因为要删除数据透视缓存,否则每次运行代码时,它都会抛出错误,因为您不能有两个同名的工作表,并且每次还将创建新的数据透视缓存,这将不必要地增加编号。工作簿中的数据透视缓存,因此文件的大小。
    • @sktneer 你是对的,有很多可能的错误处理要做,但首先我认为 PO 需要解决他的代码中的关键错误。但是,由于您的建议,我添加了它(另一种方式)
    • 感谢您调整它。但我认为第一点仍未解决,即您仍在使用 Set ptTable = wsSheet.PivotTables.Add(PivotCache:=cCache, TableDestination:=sSheet2.Range("A1"), TableName:="SalesPivotTable")。所以这里的wsSheet是用了错误的变量,应该用sSheet2代替。
    • @sktneer 不错,这是我正在运行的其他代码的变量(剩余),谢谢 :)
    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 2018-03-29
    • 2021-12-05
    • 2017-02-08
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多