VBA: Create Multiple Pivot Tables using Multiple Databases

AChuckle

New Member
Joined
May 6, 2011
Messages
20
Hi

In my program there is a loop to create 6 pivot tables based off of 6 individual tabs in a final summary tab. After it creates the first table the program errors giving me Runtime error 5: Invalid Procedure Call or Argument. The line it actually stops on is in bold red below. My first guess was that there is something wrong with the data on the tab that is selected but that does not appear to be the case.


Any streamlining of the code would be appreciated as well. This is my first attempt to create a pivot table in VBA.

Dim TabName(1 To 6), PivotFieldName As String
Dim PrintCell As String
Dim oPC As PivotCache
Dim oPT As PivotTable
Dim oWS, oWS2 As Worksheet

For I = 1 To 6
Sheets(TabName(I)).Select
Set oWS2 = ActiveSheet

UsedCol = Sheets("Summary").UsedRange.Columns.Count
UsedRow = Sheets("Summary").UsedRange.Rows.Count
UsedRowData = oWS2.UsedRange.Rows.Count

If I = 1 Or I = 2 Then
PivotFieldName = "P&L SIC Assumption"
Else
PivotFieldName = "Assumed Segment"
End If

If I = 1 Then
PrintCell = "A2"
Else
PrintCell = "A" & UsedRow + 4
End If

If UsedRowData > 2 Then
Set oPC = ActiveWorkbook.PivotCaches.Create(xlDatabase, oWS2.UsedRange)
Set oPT = oPC.CreatePivotTable(oWS.Range(PrintCell), , True)
oPT.AddFields (oPT.PivotFields(PivotFieldName).Name)
oPT = oPT.AddDataField(oPT.PivotFields("HQ Duns"), , xlCount)
oPT = oPT.AddDataField(oPT.PivotFields("Estimated Local Area Employees"), , xlSum)
Else
End If
Next
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,225,155
Messages
6,183,212
Members
453,151
Latest member
Lizamaison

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