简体   繁体   English

Excel VBA更改数据透视表的数据源

[英]Excel VBA Changing Data Source for Pivot Table

Okay, I have sifted through tons of posts and cannot get my code to run. 好的,我已经筛选了大量的帖子,无法让我的代码运行。 I am trying to create a reconciliation report, which I have running just fine. 我正在尝试创建一个和解报告,我已经运行得很好。 The problem comes when I am trying to take each month's report and paste it into a master reconciliation file. 当我尝试将每个月的报告并将其粘贴到主对帐文件中时,问题就出现了。 I have everything working perfectly, except I cannot for the life of me figure out WHY my pivot table source won't change. 我的一切都很完美,除了我不能为我的生活弄清楚为什么我的数据透视表源不会改变。

The error I am getting with the code as is: 我得到的代码错误是:

Run-time error '1004': Application-defined or object-defined error 运行时错误'1004':应用程序定义的错误或对象定义的错误

Any suggestions? 有什么建议? :-\\ : - \\

    Dim SrcData As String
    Dim PivTbl As PivotTable

    SrcData = ActiveSheet.Name & "!" & Range("$A$1:$H$" & LastRow).Address(ReferenceStyle:=xlR1C1)

    On Error Resume Next
        Set PivTbl = Sheets("Report").PivotTables("ReportPivot")
    On Error GoTo 0
        If PivTbl Is Nothing Then
            'create pivot
        Else
            ActiveWorkbook.PivotTables(PivTbl).ChangePivotCache ActiveWorkbook. _
            PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData, _
            Version:=xlPivotTableVersion15)

            PivTbl.RefreshTable
        End If

Since the problem is here: 既然问题在这里:

 ActiveWorkbook.PivotTables(PivTbl).ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData, _
        Version:=xlPivotTableVersion15)

I guess you cannot get a pivot table passing the table itself as argument: .PivotTables(PivTbl) 我想你不能得到一个数据透视表传递表本身作为参数: .PivotTables(PivTbl)

PivTbl is already the table you want. PivTbl已经是您想要的表。

PivTbl.ChangePivotCache.......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM