Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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_Pivot Table - Fatal编程技术网

Excel VBA:在特定目标中创建数据透视表时是否没有错误?

Excel VBA:在特定目标中创建数据透视表时是否没有错误?,vba,excel,pivot-table,Vba,Excel,Pivot Table,我一直在广泛地研究如何通过vba创建透视表,但遇到了一些无法解决的复杂问题。我正在尝试在工作表透视的第4列中创建透视表。当我尝试运行代码时,我得到: 运行时错误1004:工作表类的PivotTableWizard方法失败 有人能帮忙吗?我对vba还是很陌生。 这是我的代码,我一直在第二行出错: Sub PivotTable() ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("Informati

我一直在广泛地研究如何通过vba创建透视表,但遇到了一些无法解决的复杂问题。我正在尝试在工作表透视的第4列中创建透视表。当我尝试运行代码时,我得到:

运行时错误1004:工作表类的PivotTableWizard方法失败

有人能帮忙吗?我对vba还是很陌生。 这是我的代码,我一直在第二行出错:

 Sub PivotTable()
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("Information").UsedRange).CreatePivotTable TableDestination:="Pivot!R1C4", TableName:="PivotTable", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1)
With ActiveSheet.PivotTables("PivotTable").PivotFields("PN")
  .Orientation = xlRowField
 .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Commit")
 .Orientation = xlColumnField
 .Position = 1
End With
ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables("PivotTable").PivotFields("Qty"), "Sum", xlSum
End Sub

您正在代码注释中查找类似以下代码的说明:

Option Explicit

Sub AutoPivotTable()

Dim Sht As Worksheet
Dim PvtSht As Worksheet
Dim SrcData As Range
Dim PvtCache As PivotCache
Dim PvtTbl As PivotTable

'-- Determine the data range you want to pivot --
' Set the Worksheet object
Set Sht = ThisWorkbook.Worksheets("Information")
Set SrcData = Sht.UsedRange '1:Z10000").Address(False, False, xlR1C1, xlExternal)

' Set the Pivot Cache from Source Data
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData.Address(False, False, xlA1, xlExternal))

' Set the Worksheet object where the Pivot Table will be loated
Set PvtSht = ThisWorkbook.Worksheets("Pivot")

On Error Resume Next
'Set the pivot table object
Set PvtTbl = PvtSht.PivotTables("PivotTable") ' check if "PivotTable" Pivot Table already created (in past runs of this Macro)
On Error GoTo 0
If PvtTbl Is Nothing Then '<-- Pivot Table not created >> create it
    ' create a new Pivot Table in "Pivot" sheet
    Set PvtTbl = PvtSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PvtSht.Range("D1"), TableName:="PivotTable")
    With PvtTbl
        With .PivotFields("PN")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("Commit")
            .Orientation = xlColumnField
            .Position = 1
        End With
        .AddDataField .PivotFields("Qty"), "Sum of Qty", xlSum
    End With

Else ' just refresh the Pivot cache with the updated Range
    PvtTbl.ChangePivotCache PvtCache
    PvtTbl.RefreshTable
End If

End Sub

当您已经在第一行中指定了在何处创建该行时,该行的意义是什么?正如@Rory所提到的。。。看起来您已经创建了名为“数据透视表”的数据透视表。注释掉第二行,并逐步完成。看看当你打到第三条线时会发生什么。只要您的初始行使用您想要的数据创建PT,它就应该继续。我猜您是通过使用Record宏获得的,这是一个很好的开始。尝试在不使用向导的情况下录制宏刷新。它可以帮助您了解如何严格使用VBA创建PT。