PC Review


Reply
Thread Tools Rate Thread

Automate Pivot Creation

 
 
acss
Guest
Posts: n/a
 
      21st Apr 2008
Not very familiar with macros in excell but here goes. I need to automate
excel steps done monthly. I use a template workbook containing two sheets
that is filled with data during the month then a pivot table is created for
each sheet . Can the steps in creating the pivots be automated?
 
Reply With Quote
 
 
 
 
ryguy7272
Guest
Posts: n/a
 
      22nd Apr 2008
Absolutely! Turn on the macro recorder and then create your Pivot Table.
Assign this to a Control Button to run it when needed.

I'll provide a sample of code that I use, but please understand that your
code will be quite different...I provide it only as a guideline...

Sub PivotTableInputs()


Sheets("MergeSheet").Select

Cells.Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Sheets("MergeSheet").Range("A1").CurrentRegion).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

With ActiveSheet.PivotTables("PivotTable1").PivotFields("State")
.PivotItems("(blank)").Visible = False
End With

With
ActiveSheet.PivotTables("PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value)
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value),
"Count of ", xlCount

With
ActiveSheet.PivotTables("PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value)
.Orientation = xlRowField
.Position = 1
End With

ActiveSheet.PivotTables("PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value).AutoSort _
xlDescending, "Count of "
ActiveSheet.Name = Range("MRG").Value & " - Pivot"


Charts.Add
With ActiveChart.ChartGroups(1)
.Overlap = 100
.GapWidth = 150
.HasSeriesLines = False
.VaryByCategories = False

ActiveChart.ChartTitle.Select
Selection.Text = "=Summary!R5C3"
'ActiveChart.ChartArea.Select
ActiveSheet.Name = Range("MRG").Value & " - Chart"
End With

Selection.Text = "=Summary!R5C3"
ActiveChart.ChartArea.Select
End Sub

The most important part is this:

Cells.Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Sheets("MergeSheet").Range("A1").CurrentRegion).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

That is how you select the entire range of input data for the Pivot Table.

Regards,
Ryan---

--
RyGuy


"acss" wrote:

> Not very familiar with macros in excell but here goes. I need to automate
> excel steps done monthly. I use a template workbook containing two sheets
> that is filled with data during the month then a pivot table is created for
> each sheet . Can the steps in creating the pivots be automated?

 
Reply With Quote
 
acss
Guest
Posts: n/a
 
      23rd Apr 2008
Thanks. In this workbook, there are two worksheets with each one needing
their own pivot table so in the end result is a workbook with 2 sheets of
data and two pivots. Do i use the recorder twice or do i begin recording and
perform the pivots for both sheets at the same time?

"ryguy7272" wrote:

> Absolutely! Turn on the macro recorder and then create your Pivot Table.
> Assign this to a Control Button to run it when needed.
>
> I'll provide a sample of code that I use, but please understand that your
> code will be quite different...I provide it only as a guideline...
>
> Sub PivotTableInputs()
>
>
> Sheets("MergeSheet").Select
>
> Cells.Select
> ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> Sheets("MergeSheet").Range("A1").CurrentRegion).CreatePivotTable _
> TableDestination:="", TableName:="PivotTable1", _
> DefaultVersion:=xlPivotTableVersion10
>
> With ActiveSheet.PivotTables("PivotTable1").PivotFields("State")
> .PivotItems("(blank)").Visible = False
> End With
>
> With
> ActiveSheet.PivotTables("PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value)
> .Orientation = xlRowField
> .Position = 1
> End With
> ActiveSheet.PivotTables("PivotTable1").AddDataField
> ActiveSheet.PivotTables( _
> "PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value),
> "Count of ", xlCount
>
> With
> ActiveSheet.PivotTables("PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value)
> .Orientation = xlRowField
> .Position = 1
> End With
>
> ActiveSheet.PivotTables("PivotTable1").PivotFields(Sheets("Summary").Range("C5").Value).AutoSort _
> xlDescending, "Count of "
> ActiveSheet.Name = Range("MRG").Value & " - Pivot"
>
>
> Charts.Add
> With ActiveChart.ChartGroups(1)
> .Overlap = 100
> .GapWidth = 150
> .HasSeriesLines = False
> .VaryByCategories = False
>
> ActiveChart.ChartTitle.Select
> Selection.Text = "=Summary!R5C3"
> 'ActiveChart.ChartArea.Select
> ActiveSheet.Name = Range("MRG").Value & " - Chart"
> End With
>
> Selection.Text = "=Summary!R5C3"
> ActiveChart.ChartArea.Select
> End Sub
>
> The most important part is this:
>
> Cells.Select
> ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
> Sheets("MergeSheet").Range("A1").CurrentRegion).CreatePivotTable _
> TableDestination:="", TableName:="PivotTable1", _
> DefaultVersion:=xlPivotTableVersion10
>
> That is how you select the entire range of input data for the Pivot Table.
>
> Regards,
> Ryan---
>
> --
> RyGuy
>
>
> "acss" wrote:
>
> > Not very familiar with macros in excell but here goes. I need to automate
> > excel steps done monthly. I use a template workbook containing two sheets
> > that is filled with data during the month then a pivot table is created for
> > each sheet . Can the steps in creating the pivots be automated?

 
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
Automate PDF creation oguime@gmail.com Microsoft Excel Programming 3 29th Aug 2006 04:26 PM
Can I automate the creation of the mdw file? A C Microsoft Access Security 1 8th Dec 2005 12:13 PM
HOW DO I AUTOMATE CREATION OF JOB SHEETS? bobby smith Microsoft Excel Programming 2 15th Jun 2005 12:06 AM
automate new folder creation Doug Windows XP General 2 20th Apr 2005 01:32 PM
Automate creation of subfolders Angie Microsoft Outlook VBA Programming 1 20th Aug 2003 02:51 PM


Features
 

Advertising
 

Newsgroups
 


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