2017-04-25 104 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 **) –