2017-04-25 65 views
1

我想創建一個宏使用的代碼在以下網站建立數據透視表:VBA,數據透視表嚮導方法

http://msdn.microsoft.com/en-us/library/office/hh243933.aspx

,但我一直得到

錯誤1004:「無法獲得樞軸表的樞軸特性 CLASS「

關於re的任何建議在這個問題背後有什麼解決辦法?

這是我的代碼

Sub CreatePivot() 

' Creates a PivotTable report from the table on Sheet1 
' by using the PivotTableWizard method with the PivotFields 
' method to specify the fields in the PivotTable. 

Dim objTable As PivotTable, objField As PivotField 

' Select the sheet and first cell of the table that contains the data. 
ActiveWorkbook.Sheets("Sheet1").Select 
Range("A2").Select 

' Create the PivotTable object based on the Employee data on Sheet1. 
Set objTable = Sheet1.PivotTableWizard 

' Specify row and column fields. 
***Set objField = objTable.PivotFields("v1")*** ' <-- This where I get the Error 
objField.Orientation = xlColumnField 

Set objField = objTable.PivotFields("Temperature") 
objField.Orientation = xlRowField 

' Specify a data field with its summary 
' function and format. 
Set objField = objTable.PivotFields("clkui") 
objField.Orientation = xlDataField 
objField.Function = xlSum 
objField.NumberFormat = "$ #,##0" 

' Specify a page field. 
Set objField = objTable.PivotFields("db") 
objField.Orientation = xlPageField 

' Preview the new PivotTable report. 
ActiveSheet.PrintPreview 

' Prompt the user whether to delete the PivotTable. 
Application.DisplayAlerts = False 
If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then 
    ActiveSheet.Delete 
End If 
Application.DisplayAlerts = True 

End Sub 

enter image description here

+0

是在「Sheet1」中創建的'PivotTable'嗎?或不 ? 「數據透視表」的數據在哪裏?哪張工作表?範圍 ? –

+0

no.the pivottable not created.the data is in sheet1 – ofir

+0

嘗試我的回答下面的代碼 –

回答

0

嘗試下面的代碼在Sheet1創建一個新表從「工作表Sheet1」中的數據(不知道什麼是表的名字):

Option Explicit 

Sub CreatePivot() 

' Creates a PivotTable report from the table on Sheet1 
' by using the PivotTableWizard method with the PivotFields 
' method to specify the fields in the PivotTable. 

Dim objTable As PivotTable 
Dim objTblCache As PivotCache 
Dim objField As PivotField 
Dim PvtSht As Worksheet 
Dim LastRow As Long, LastCol As Long 
Dim SrcRng As Range 

' Select the sheet and first cell of the table that contains the data 
With ThisWorkbook.Sheets("Sheet1") 
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column 

    Set SrcRng = .Range(.Cells(2, "A"), .Cells(LastRow, LastCol)) ' <-- set the Pivot's dynamic Range (starts from "A2") 
End With 

' Option 1: Define Pivot Cache 
Set objTblCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, SrcRng) 

' Option 2: Define Pivot Cache 
Set objTblCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRng.Address(True, True, xlA1, True)) 

' Create the PivotTable object based on the Employee data on Sheet1. 
Set objTable = Sheet1.PivotTables.Add(PivotCache:=objTblCache, TableDestination:=Sheet1.Cells(1, LastCol + 2), TableName:="PivotTable1") 

' Specify row and column fields. 
With objTable 
    .PivotFields("v1").Orientation = xlColumnField 
    .PivotFields("Temperature").Orientation = xlRowField 

    ' Specify a data field with its summary 
    ' function and format. 
    Set objField = .PivotFields("clkui") 
    With objField 
     .Orientation = xlDataField 
     .Function = xlSum 
     .NumberFormat = "$ #,##0" 
    End With 

    ' Specify a page field. 
    .PivotFields("db").Orientation = xlPageField 
End With 

' rest of your code goes here .... 


End Sub 
+0

我意識到有兩種方法來創建pivottable:1.withPivotTableWizard方法.2。與樞軸緩存。 – ofir

+0

我意識到有兩種創建pivottable的方法:1.withPivotTableWizard method.2。與樞軸緩存。我想用數據透視表嚮導來做到這一點。 「Set objTable = Sheet1.PivotTableWizard」這一行正在工作,因此在我的代碼 – ofir

+0

@ofir中創建了pivot,但它的get stack在「Set objField = objTable.PivotFields(」v1「)」中我從來沒有在VBA中使用過你的方式遠遠沒有遇到過在這裏使用它的一次嘗試** SO **) –