【问题标题】:Having issues with creating pivot table with VBA使用 VBA 创建数据透视表时遇到问题
【发布时间】:2018-02-21 03:33:50
【问题描述】:

所以我已经能够在某种程度上创建一个数据透视表,我会告诉你我的意思。到目前为止,这是我的代码。

Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Dim StartPT As String
Set WSD = Worksheets("aggregateData")
Set WSD2 = Worksheets("Sheet12")


' Select the data for pivot table

FinalRow = WSD.Cells(Rows.Count, 2).End(xlUp).Row
FinalCol = WSD.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = WSD.Cells(2, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)

'Where do I want the pivot table to start

StartPT = WSD2.Range("A1").Address(ReferenceStyle:=xlR1C1)


'Begin to Create the Pivot Table

Set PT = PTCache.CreatePivotTable(TableDestination:=StartPT, TableName:="POS Data")

PT.PivotFields("Fineline").Orientation = xlRowField


End Sub

当我运行这段代码时,我得到了一个数据透视表的开头。但是,数据透视表显示为白色,而不是普通数据透视表的蓝色。任何帮助,将不胜感激。

谢谢,

G

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    请将您的代码中的这一行 Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange) 替换为此代码 Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange, Version:=xlPivotTableVersion15) 。 xlPivotTableVersion15 是标准的蓝色版本的数据透视表。请确认这是否能解决您的问题。

    【讨论】:

    • 嘿,我收到错误“编译错误:未找到命名参数”如果这意味着什么,我正在使用 Excel 2010
    • 如果您使用的是 Excel 2010,请尝试 xlPivotTableVersion14。
    • 它突出显示“版本”并告诉我错误
    • 您是否将 ActiveWorkbook.PivotCaches.Add 替换为 ActiveWorkbook.PivotCaches.Create?我正在我的 2010 Excel 上复制相同的宏,它正在工作......
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多