0

CODE IS WORKING! THANKS FOR THE HELP!

I am attempting to create a dynamic pivot table that will work on data that varies in the number of rows. Currently, I have 28,300 rows, but this may change daily.

Example of data format as follows:

Case Number    Branch      Driver
1342           NYC         Bob
4532           PHL         Jim
7391           CIN         John
8251           SAN         John
7211           SAN         Mary
9121           CLE         John
7424           CIN         John

Example of finished table:

Driver    NYC    PHL   CIN   SAN   CLE
Bob       1      0     0     0     0
Jim       0      1     0     0     0    
John      0      0     2     1     1     
Mary      0      0     0     1     0     

Code as follows:

Sub CreateSummaryReportUsingPivot()
' Use a Pivot Table to create a static summary report
' with model going down the rows and regions across
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set WSD = Worksheets("PivotTable")

'Name active worksheet as "PivotTable"
 ActiveSheet.Name = "PivotTable"

' Delete any prior pivot tables
For Each PT In WSD.PivotTables
    PT.TableRange2.Clear
Next PT

' Define input area and set up a Pivot Cache
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count). _
    End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
    xlDatabase, SourceData:=PRange)

' Create the Pivot Table from the Pivot Cache
Set PT = PTCache.CreatePivotTable(TableDestination:=WSD. _
    Cells(2, FinalCol + 2), TableName:="PivotTable1")

' Turn off updating while building the table
PT.ManualUpdate = True

' Set up the row fields
PT.AddFields RowFields:="Driver", ColumnFields:="Branch"

' Set up the data fields
With PT.PivotFields("Case Number")
    .Orientation = xlDataField
    .Function = xlCount
    .Position = 1
End With

With PT
    .ColumnGrand = False
    .RowGrand = False
    .NullString = "0"
End With

' Calc the pivot table
PT.ManualUpdate = False
PT.ManualUpdate = True

End Sub
6
  • 1
    You say this is being performed on a CSV file? What are the chances you don't have a sheet named "PivotTable"? Commented Sep 25, 2012 at 16:26
  • I thought that Dim WSD as Worksheet, then Set WSD = Worksheets("PivotTable") dynamically created the Worksheet named PivotTable. Is this not the case? Commented Sep 25, 2012 at 16:56
  • Renaming the worksheet from "Sheet1" to "PivotTable" manually and then running the macro, bypasses the orginal error, so your insight helped. Now I am erroring out in the section commented as ' Create the Pivot Table from the Pivot Cache. The error is "Method 'CreatePivotTable' of object 'PivotCache' failed" Commented Sep 25, 2012 at 17:02
  • I added ActiveSheet.Name = "PivotTable" after instantiating the variables near the top. Commented Sep 25, 2012 at 17:09
  • 1
    try pivotcache.create instead of .add Commented Sep 25, 2012 at 19:37

2 Answers 2

2

They changed the object model for PivotCaches. The method you need in 2007-2010 (that use VBA version 7 instead of version 6) is

 PivotCaches.Create
Sign up to request clarification or add additional context in comments.

Comments

1

Any reason you are using VBA other than the changing number of rows?

If you are using Excel 2007 / 2010, create a regular table /list (Ctrl-L) from your original data. You can give it a name as well. Then create a Pivot Table and use the table name as the data source. As you add rows your table will expand and you can then just refresh the Pivot Table (F5, or using VBA).

If you are in Excel 2003, you can create dynamic named ranges as well. It's slightly more complex (and a lot more ugly) but I can walk you through it if you are stuck on an older version.

6 Comments

I am using VBA, because this is an automated process that runs on a cvs file I receive daily via email. I run the macro off of a script that is triggered by email rules.I am using Excel 2011 for Mac. It has VBA 14.0 in it.
If I can get this macro to work, I can fully automate the process of collecting the data and will have daily updated info that is used as a basis for visual analytics in Tableau.
Obvious one, but is the worksheet called exactly PivotTable? Does Sheets("PivotTable").Activate bring up the sheet?
Short answer, No. The macro fails when I set WSD to the name "PivotTable".
My impression of this code was that it was supposed to create the worksheet "PivotTable" dynamically, but was failing for some reason.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.