2012-08-22 22 views
0

我創建了基於關閉的例子VBA代碼的微軟教程頁面上的子程序:LINKVBA PivotTableWizard對象

Sub WIP20Pivot() 
' 
' Pivot Macro 

'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("DATA").Select 
Range("A1").Select 

' Create the PivotTable object based on the Employee data on Sheet1. 
Set objTable = Sheet1.PivotTableWizard 'ERROR OCCURS HERE 
ActiveSheet.Name = "PivotSheet" 

'There is more VBA code below to select fields for the pivot table, etc.. 

End Sub 

我收到以下錯誤,當我運行這段代碼:

運行時間錯誤「424」:「需要對象」。

奇怪的是,子程序在微軟可下載的示例宏的教程頁上工作得很好。

在此先感謝您的幫助!

感謝, AME

+2

如果您沒有在您的工作簿的第一頁(通過索引)上的數據透視表,你會得到錯誤。更改工作表參考以適合您的特定工作簿,它將起作用。 –

回答

0

我有同樣的問題。在我做了一些研究之後,我找到了這個解決方案。希望它也能幫助你。

Set objTable = Worksheets("PivotTable").PivotTableWizard(SourceType:=xlDatabase, SourceData:=Range("PivotData!A1:C3918"), tabledestination:=Range("PivotTable!A1")) 
0

Sub WIP20Pivot() => subroutine必須是一個模塊中,

不能嵌入在工作表中。

它也發生在我身上。

0

我想這有助於命名數據​​透視表?

1

設置objTable = Sheet1.PivotTableWizard「錯誤發生時在這裏你必須指定Shee1然而真實表名稱會有所不同

。 您將刪除一些工作表,並可能將其命名爲工作表Sheet1。 請在vba編輯器中檢查項目窗口,您的實際工作表名稱將顯示爲「Sheet9(Sheet1)」。在這種情況下,你需要提及的

「設置objTable = Sheet9.PivotTableWizard」

這就是它解決了! :)

(請注意你的代碼應該是模塊)