Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Ken Ken is offline
external usenet poster
 
Posts: 590
Default Record Macro to Create Pivot Table

Hi!

I used Macro Recorder to create a Pivot Table. After recording was done, I
re-ran the Macro. Then, error message came up. The message says "Run-Time
Error '1004' AddFields Method of PivotTable Class Failed"
Please refer to the following Macro Codes:

Sub test()
'
' test Macro
' Macro recorded 04/25/2007 by METALCRAFTERS
'
' Keyboard Shortcut: Ctrl+t
'
Range("A1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _
"Data!C1:C5").CreatePivotTable TableDestination:="", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="JobNumber", _
ColumnFields:="CostType"
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("TOTAL COST")
.Orientation = xlDataField
.Caption = "Sum of TOTAL COST"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = True
ActiveWorkbook.ShowPivotTableFieldList = False
Range("D2").Select
End Sub


Highlighted Sentence:
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="JobNumber", _
ColumnFields:="CostType"

Thank you very much for your help!

Ken
  #2   Report Post  
Posted to microsoft.public.excel.misc
Ken Ken is offline
external usenet poster
 
Posts: 590
Default Record Macro to Create Pivot Table

I found the error. The error came from wrong selection of source data.
Somehow, it was recorded as SourceData:= _"Data!C1:C5" insetad of
SourceData:= _"Data!A1:E65536"

I still don't understand why Recorder only select 5 cells

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create pivot table from existing pivot table in excel 2007 Udayraj Dhulekar Excel Discussion (Misc queries) 2 July 8th 13 08:22 PM
Can I run a macro to create a pivot table? Eqa Excel Discussion (Misc queries) 2 April 8th 07 06:16 AM
how to add a record to a pivot table Rasoul Khoshravan Excel Worksheet Functions 1 October 26th 06 09:36 AM
Record Pivot Table Macro Corey Excel Discussion (Misc queries) 2 February 11th 06 01:31 AM
How do I create a pivot table if the pivot table icon or menu ite. Lynn@WS Charts and Charting in Excel 1 December 16th 04 02:36 AM


All times are GMT +1. The time now is 12:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"