Macro Runtime Errors When Creating Pivot Tables

kkunert

New Member
Joined
Sep 11, 2010
Messages
4
Hi, I am neophyte in the world of macros, but I badly need their productivity value to help my small business.

I have single Excel 2007 workbooks with five worksheets. On each of these worksheets are large amounts of data relating to M&A transactions. I can have as many as 5,000 rows and 47 columns. The worksheets correspond to a single quarter in a yearm so 1Q00, 2Q00, 3Q00, 4Q00 and a full year worksheet FY00.

A pivot table can be used on the data in each worksheet to break down the count and aggregate value of M&A activity in a given quarter of full year based on an industry label of sector label.

The pivot tables are great, but I have many reports to do over the period of 2000-2010 for the U.S., Europe, Asia, North America, and Global. I am hoping to speed up the pivot table process by using macros. However, I am encountering constant errors when I try to record and run a macro. I create tables out of my data and then record exactly how I want my pivot table to look, then I stop the recording.

But when I test the macro, it invariably pops us an error of runtime 5, runtime 1004, a range problem or an inability to get PivotFields property of PivotTable Class.

This is debug highlight:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion12).CreatePivotTable TableDestination _
:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion12


This is the VB code:

Sub indpiv()
'
' indpiv Macro
'
' Keyboard Shortcut: Ctrl+l
'
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion12).CreatePivotTable TableDestination _
:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion12
Sheets("Sheet1").Select
Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields( _
"Announced/Initial Filing Date (Including Bids and Letters of Intent)"), _
"Count of Announced/Initial Filing Date (Including Bids and Letters of Intent)" _
, xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Total Transaction Value ($USDmm, Historical rate)") _
, "Sum of Total Transaction Value ($USDmm, Historical rate)", xlSum
With ActiveSheet.PivotTables("PivotTable1").DataPivotField
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Primary Industry [Target/Issuer]")
.Orientation = xlColumnField
.Position = 1
End With
End Sub


Does anyone see an error that stands out to them?

Any help would be greatly appreciated.

Kurt
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try something like this...

Code:
Sub indpiv()
'
' indpiv Macro
'
' Keyboard Shortcut: Ctrl+l
'
    Dim rngDATA As Range, wsPVT As Worksheet, ptNew As PivotTable
    

    Set rngDATA = Sheets("Sheet1").Range("A1").CurrentRegion
        
    Set wsPVT = Sheets.Add(After:=Sheets(Sheets.Count))
    
    Set ptNew = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=rngDATA) _
                              .CreatePivotTable(TableDestination:=wsPVT.Range("A3"))
    
    With ptNew
    
        .AddDataField .PivotFields("Announced/Initial Filing Date (Including Bids and Letters of Intent)"), _
                                   "Count of Announced/Initial Filing Date (Including Bids and Letters of Intent)", xlCount
                                   
        .AddDataField .PivotFields("Total Transaction Value ($USDmm, Historical rate)"), _
                                   "Sum of Total Transaction Value ($USDmm, Historical rate)", xlSum
                                   
        With .DataPivotField
            .Orientation = xlRowField
            .Position = 1
        End With
        
'        With .PivotFields("Primary Industry [Target/Issuer]")
'            .Orientation = xlColumnField
'            .Position = 1
'        End With
        
    End With
    
End Sub

I couldn't figure out what you wanted to do with...
With .PivotFields("Primary Industry [Target/Issuer]")
...so I commented it out.

Your macro doesn't create a "Primary Industry [Target/Issuer]" pivot table data field.
 
Upvote 0
AlphaFrog,

Thanks very much for the reply. I tried what you suggested. Unfortunately, when I run it on a table, it creates a runtime error 9 - subscript out of range.
 
Upvote 0
Hi, this is the highlighted line of code when debugging:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion12).CreatePivotTable TableDestination _
:="Sheet1!R3C1", TableName:="PivotTable12", DefaultVersion:= _
xlPivotTableVersion12

Your help is much appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top