Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default pivot tables criteria vba

I need to use vba to make a pivot table's criteria equal the contents of a
cell. Any code/help would be very much appreciated.

Thank you,
Nick Cherry
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default pivot tables criteria vba

Nick,

Here is a macro I wrote which creates a pivot table from Excel data.
Currently, it has 3 row fields, 1 column field, and 1 data field. I tweaked
it to read the field names from cells A5 - E5 (using the contents of those
cells as criteria for the pivot table.) Also, as written, it takes data from
Sheet1 and creates the pivot table on a new sheet Test1.

Sub MakePvtTbl()
'Creates a pivot table from outstanding records. !!! ACTIVECELL MUST BE
ANY CELL IN THE DATA !!!
Dim Sorce As Range, Dest As Range, msg5 As String, strFld As String
Const PvtTbl = "Test1"
On Error GoTo MPTerr5
'Select all the cells with data as the source (Sorce object) for the pivot
table.
ActiveCell.CurrentRegion.Select
Set Sorce = Selection
'Delete the existing PvtTbl sheet, then create a new one.
On Error Resume Next
Application.DisplayAlerts = False
ActiveWorkbook.Sheets(PvtTbl$).Delete
Application.DisplayAlerts = True
Worksheets.Add.Move Befo=Sheet1
ActiveSheet.Name = PvtTbl$
On Error GoTo MPTerr5
'Set the object Dest to be cell A3 on the new sheet. That's where the pivot
table will be created.
Set Dest = ActiveSheet.Range("A3")
On Error GoTo MPTerr5
'Call the PivotTableWizard
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=Sorce,
TableDestination:=Dest, TableName:="PivotTable1"
'Define the row fields.
strFld$ = ActiveWorkbook.Sheets("Sheet1").Range("C5").Value
With ActiveSheet.PivotTables("PivotTable1").PivotFields (strFld$)
.Orientation = xlRowField
.Position = 1
End With
strFld$ = ActiveWorkbook.Sheets("Sheet1").Range("A5").Value
With ActiveSheet.PivotTables("PivotTable1").PivotFields (strFld$)
.Orientation = xlRowField
.Position = 2
End With
strFld$ = ActiveWorkbook.Sheets("Sheet1").Range("B5").Value
With ActiveSheet.PivotTables("PivotTable1").PivotFields (strFld$)
.Orientation = xlRowField
.Position = 3
End With
'Define the column field.
strFld$ = ActiveWorkbook.Sheets("Sheet1").Range("D5").Value
With ActiveSheet.PivotTables("PivotTable1").PivotFields (strFld$)
.Orientation = xlColumnField
.Position = 1
End With
'Define the data field.
strFld$ = ActiveWorkbook.Sheets("Sheet1").Range("E5").Value
With ActiveSheet.PivotTables("PivotTable1").PivotFields (strFld$)
.Orientation = xlDataField
.Position = 1
End With
ActiveSheet.Range("A3").Select
Cleanup5:
Set Sorce = Nothing
Set Dest = Nothing
Exit Sub
MPTerr5:
If Err.Number < 0 Then
msg5$ = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
MsgBox msg5$, , "MakePvtTbl error", Err.HelpFile, Err.HelpContext
End If
GoTo Cleanup5
End Sub

I hope this is helpful.

Hutch
------------------------------------------------------
"cherrynich" wrote:

I need to use vba to make a pivot table's criteria equal the contents of a
cell. Any code/help would be very much appreciated.

Thank you,
Nick Cherry

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
Building pivot tables in Excel 2007 based on existing pivot tables? [email protected] Excel Discussion (Misc queries) 4 December 26th 07 08:05 PM
Filtering by criteria within Pivot Tables in Excel 2000 Sardonic Excel Discussion (Misc queries) 0 December 4th 07 02:55 PM
Date range criteria and Pivot tables (again!) DerbyJim1978 Excel Worksheet Functions 3 July 13th 05 10:14 PM
How does the term 'pivot' apply to Excel's Pivot tables and Pivot. stvermont Excel Discussion (Misc queries) 1 February 17th 05 01:34 AM
Pivot tables-controlling user interaction with pivot tables Sindhura Excel Programming 0 August 27th 03 02:10 PM


All times are GMT +1. The time now is 06:55 AM.

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

About Us

"It's about Microsoft Excel"