【问题标题】:Excel VBA Macro for Pivot Table with Dynamic Data Range具有动态数据范围的数据透视表的 Excel VBA 宏
【发布时间】:2014-07-14 15:13:54
【问题描述】:

代码正在运行!感谢您的帮助!

我正在尝试创建一个动态数据透视表,该表将处理行数不同的数据。目前,我有 28,300 行,但这可能每天都在变化。

数据格式示例如下:

Case Number    Branch      Driver
1342           NYC         Bob
4532           PHL         Jim
7391           CIN         John
8251           SAN         John
7211           SAN         Mary
9121           CLE         John
7424           CIN         John

成品表示例:

Driver    NYC    PHL   CIN   SAN   CLE
Bob       1      0     0     0     0
Jim       0      1     0     0     0    
John      0      0     2     1     1     
Mary      0      0     0     1     0     

代码如下:

Sub CreateSummaryReportUsingPivot()
' Use a Pivot Table to create a static summary report
' with model going down the rows and regions across
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set WSD = Worksheets("PivotTable")

'Name active worksheet as "PivotTable"
 ActiveSheet.Name = "PivotTable"

' Delete any prior pivot tables
For Each PT In WSD.PivotTables
    PT.TableRange2.Clear
Next PT

' Define input area and set up a Pivot Cache
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count). _
    End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
    xlDatabase, SourceData:=PRange)

' Create the Pivot Table from the Pivot Cache
Set PT = PTCache.CreatePivotTable(TableDestination:=WSD. _
    Cells(2, FinalCol + 2), TableName:="PivotTable1")

' Turn off updating while building the table
PT.ManualUpdate = True

' Set up the row fields
PT.AddFields RowFields:="Driver", ColumnFields:="Branch"

' Set up the data fields
With PT.PivotFields("Case Number")
    .Orientation = xlDataField
    .Function = xlCount
    .Position = 1
End With

With PT
    .ColumnGrand = False
    .RowGrand = False
    .NullString = "0"
End With

' Calc the pivot table
PT.ManualUpdate = False
PT.ManualUpdate = True

End Sub

【问题讨论】:

  • 您说这是在 CSV 文件上执行的?您没有名为“数据透视表”的工作表的可能性有多大?
  • 我以为 Dim WSD as Worksheet,然后 Set WSD = Worksheets("PivotTable") 动态创建了名为 PivotTable 的 Worksheet。不是这样吗?
  • 手动将工作表从“Sheet1”重命名为“数据透视表”,然后运行宏,绕过了原始错误,因此您的洞察力有所帮助。现在我在注释为“从数据透视缓存创建数据透视表”的部分中出错了。错误是“对象'PivotCache'的方法'CreatePivotTable'失败”
  • 我在顶部附近实例化变量后添加了 ActiveSheet.Name = "PivotTable"。
  • 尝试使用 pivotcache.create 代替 .add

标签: macos excel pivot-table vba


【解决方案1】:

他们更改了 PivotCache 的对象模型。您在 2007-2010 年需要的方法(使用 VBA 版本 7 而不是版本 6)是

 PivotCaches.Create

【讨论】:

    【解决方案2】:

    除了改变行数之外,还有什么原因使用 VBA?

    如果您使用的是 Excel 2007 / 2010,请根据您的原始数据创建一个常规表格/列表 (Ctrl-L)。你也可以给它一个名字。然后创建一个数据透视表并使用表名作为数据源。当您添加行时,您的表格将展开,然后您可以刷新数据透视表(F5,或使用 VBA)。

    如果您使用的是 Excel 2003,也可以创建动态命名范围。它稍微复杂一些(而且更丑陋),但如果您卡在旧版本上,我可以指导您完成。

    【讨论】:

    • 我正在使用 VBA,因为这是一个自动化过程,运行在我每天通过电子邮件收到的 cvs 文件上。我从电子邮件规则触发的脚本中运行宏。我使用的是 Excel 2011 for Mac。它包含 VBA 14.0。
    • 如果我能让这个宏工作,我可以完全自动化收集数据的过程,并且每天都会更新信息,用作 Tableau 中可视化分析的基础。
    • 很明显,但是工作表是否完全称为数据透视表? Sheets("PivotTable").Activate 会调出工作表吗?
    • 简短回答,不。当我将 WSD 设置为名称“数据透视表”时,宏失败。
    • 我对这段代码的印象是它应该动态创建工作表“数据透视表”,但由于某种原因失败了。
    猜你喜欢
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    相关资源
    最近更新 更多