Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excel VBA中创建透视表?_Vba_Excel - Fatal编程技术网

如何在excel VBA中创建透视表?

如何在excel VBA中创建透视表?,vba,excel,Vba,Excel,我试图创建一个透视表,但最后一行代码出错 Dim WSD As Worksheet Dim WSD2 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("SKU Sum") Set WSD2 = Worksheets("Finelines") ' Select

我试图创建一个透视表,但最后一行代码出错

Dim WSD As Worksheet
Dim WSD2 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("SKU Sum")
Set WSD2 = Worksheets("Finelines")

' Select the data for pivot table

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

Set PT = WSD.PivotTables.Add(PivotCache:=PTCache, TableDestination:=WSD2.Range(A1), TableName:="Pivotab")
任何帮助都将不胜感激

谢谢


G

将最后一行替换为

Set PT = PTCache.CreatePivotTable(TableDestination:=WSD2.Range("A1"), TableName:="Pivotab")

替换当前的
PivtoCache
行:

Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
与:


同样,您在最后一行中得到了错误,因为您正在设置透视表的位置
WSD2.Range(A1)
,括号内的字符串需要有
。因此也将其修改为
WSD2.Range(“A1”)

您的错误是什么?很抱歉,响应太晚了。但这非常有效
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange.Address(False, False, xlA1, xlExternal))