PC Review


Reply
Thread Tools Rate Thread

Creating a Pivot table

 
 
Jodie
Guest
Posts: n/a
 
      20th Nov 2009
I am trying to write a macro to create a pivot table to use in multiple files
where each file will contain the same headers and number of columns.
However, the number of rows will vary in each file. Each file has one sheet
and all of these sheets have a different name. Can anyone please help?
--
Thank you, Jodie
 
Reply With Quote
 
 
 
 
Chuck
Guest
Posts: n/a
 
      20th Nov 2009
On Nov 20, 1:19*pm, Jodie <Jo...@discussions.microsoft.com> wrote:
> I am trying to write a macro to create a pivot table to use in multiple files
> where each file will contain the same headers and number of columns. *
> However, the number of rows will vary in each file. *Each file has one sheet
> and all of these sheets have a different name. *Can anyone please help?
> --
> Thank you, Jodie


Jodie,

Here is an example set of data that I used.

1 A B C
2 Product Month Sales
3 Animal Jan-09 50
4 Vegetable Jan-09 20
5 Mineral Jan-09 10
6 Animal Feb-09 8
7 Vegetable Feb-09 50
8 Mineral Feb-09 20
9 Animal Mar-09 6
10 Vegetable Mar-09 10
11 Mineral Mar-09 18

Record a macro to create your pivot table. Here is the macro that I
recorded for this data set.

Sub Macro()

Range("A1:C10").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:= _
"Sheet1!R1C1:R10C3").CreatePivotTable TableDestination:="",
TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells
(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Product")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sales"), "Sum of Sales", xlSum

End Sub

Now, add code to the top of this to find the last row (For this
example I'm using column A) , change the Range Select statment to
select A1 and change the SourceData statement to use the rw variable
to detemine the last row.

Sub Macro()

Dim rw As Integer
' get the LAST cell
rw = Range("A65000").End(xlUp).Row

Range("A1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:= _
"Sheet1!R1C1:R" & rw & "C3").CreatePivotTable
TableDestination:="", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells
(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Product")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sales"), "Sum of Sales", xlSum

End Sub

If you have trouble with this. Record your pivot table macro and post
it.

Chuck
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      20th Nov 2009
' Assuming your data starts in column A, with headers in row 1
' and the column has no blanks
' Once the file is open

Sheet_Name = ActiveSheet.Name
Last_Row = ActiveSheet.Range("A65536").End(xlUp).Row
Your_Last_Column = 10 ' amaned this to the column number of your last
column
Pivot_Name = "YOUR PIVOT TABLE NAME"
Source_Range = "'" + Sheet_Name + "'!R1C1:R" + CStr(Last_Row) + "C" +
CStr(Your_Last_Column)
'
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Source_Range).CreatePivotTable TableDestination:= _
"", TableName:=Pivot_Name, DefaultVersion:=xlPivotTableVersion10

I am assuming that you've got the rest of the pivot routine and it's just
the range that's an issue.

"Jodie" wrote:

> I am trying to write a macro to create a pivot table to use in multiple files
> where each file will contain the same headers and number of columns.
> However, the number of rows will vary in each file. Each file has one sheet
> and all of these sheets have a different name. Can anyone please help?
> --
> Thank you, Jodie

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I detail Pivot Table data without creating a table (Excel2007) Skeletiko Microsoft Excel Misc 1 5th May 2010 08:22 AM
How can I detail Pivot Table data without creating a table (Excel2007) Skeletiko Microsoft Excel Misc 0 5th May 2010 12:21 AM
How can I detail Pivot Table data without creating a table (Excel2007)? Skeletiko Microsoft Excel Discussion 0 15th Apr 2010 11:56 AM
creating a pivot table from 4 pivot tables phyllis W Microsoft Excel Worksheet Functions 0 12th Oct 2008 09:52 PM
Probs with creating multiple pivot charts from pivot table Retreatgal Microsoft Excel Charting 2 28th Jan 2004 02:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:26 AM.