Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2007, 03:29 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
Default VBA for Pivot Tables

Hi All!
First post on the Excel Forum.

I've had quite a bit of experience with using VBA for Access, but haven't gotten into VBA with Excel.

I've been assigned the task of making multiple pivot tables based on a sheet of given data.

The program will start with 1 sheet in the workbook having 20 something rows of data. I want to automate this workbook somehow, probably via VBA, to spit out a couple of pivot charts that give different views of the data. I need these charts to be put on seperate sheets in the workbook.

I've been surfing the net some looking for ways to do this, but most sites don't go very indepth with VBA.

Any advise, strategy, or ideas are greatly appreciated.

Thanks,
David

 
Old March 21st, 2007, 01:15 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Pivot tables are a breed unto themselves and will take a lot of explanation. For ease of explaining we'll assume that one column is called "Row Data", one column is called "Column Data" and one row is called "Page Data". We'll assume you have 20 rows of data and 10 columns (A-J).

---------------------------------------------------------------------------------------
  dim oTarget As Worksheet
  ActiveWorkbook.Sheets.Add After:="Sheet1"
  ActiveWorkbook.Activesheet.Name = "Pivot A" 'Whatever you want tab name to be
  set oTarget = ActiveWorkbook.Worksheets("Pivot A")
  ActiveWorkbook.PivotCaches.Add( _
    SourceType:=xlDatabase, _
    SourceData:= "'Sheet1'!A1:J20").CreatePivotTable _
    TableDestination:=oTarget.Range("A3"), _
    TableName:="PivotA"
  oTarget.PivotTables("PivotA").SmallGrid = False 'Never found a good use for true yet
  oTarget.PivotTables("PivotA").AddFields RowFields:="Row Data", ColumnFields:="Column Data", PageFields:="Page Data"
  With oTarget.PivotTables("PivotA").PivotFields("Page Data") 'Orientation for data fields has to be set manually
    .Orientation = xlDataField
    .Caption = "Count of Page Data"
    .Function = xlCount 'Count of rows that match set criteria.
  End With
---------------------------------------------------------------------------------------

Pivot tables are really complex and there is a lot more that you can do, but this should get you started to the point that VBA help and fiddling will get you the results you want.

Hope this helps.
 
Old March 21st, 2007, 01:49 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a bunch allenm!

I'm gonna give this a shot.

 
Old March 21st, 2007, 04:01 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

No problem. I just edited my message and updated the code. I missed a " mark for the SourceData:= line.

My bad, sorry if it confused you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to work pivot tables malli_kv2 Access 1 March 16th, 2007 06:34 AM
Formula in Pivot tables JPC Excel VBA 1 February 20th, 2007 01:27 PM
Linking pivot tables iacon Excel VBA 3 July 24th, 2006 01:25 AM
International Pivot Tables noldrini Excel VBA 0 February 8th, 2006 12:54 PM
Pivot Tables smartgir Excel VBA 0 May 12th, 2004 10:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.