Creating multiple PivotTables in a wb using multiple data sources (VBA/XL07/Vista)

y2gordo

New Member
Joined
Aug 27, 2010
Messages
4
Howdy folks,

I've been working on this problem for quite a while, and I can't get it to work properly. I am creating a macro for someone where data is consolidated from multiple worksheets into two summary sheets. From there, based on options selected on a "setup" worksheet, multiple PivotTables will be created from those summary sheets (and later charts created from those PvtTbls, but I'm not there yet).

Anyway, I am at the point where I can get the program to create the first PivotTable properly. However, the program is locking up with an "Application-defined or Object-defined error" when I get to the point of creating the second table.

What is posted below is the code in its current form. I created the arrays thinking that Excel might need to place the Caches and arrays in separate elements. Previously, I repeated a "ActiveSheet.PivotCaches.Create( . . . ).CreatePivotTable . . . line in the second section and received the same error.

Code:
Function CreatePivotTables(ByVal outputSheet As String, ByVal rptTypes As String, ByVal chartSplitCat As String, ByVal exCatCount As Integer, ByVal sheetType As String, ByVal fieldType As String, ByRef extraCats() As String) As Integer
' This sub creates the PivotTables from the Output Sheet
    Dim i, j, k, l As Integer
    Dim splitRows() As Integer     ' This stores the row numbers where the data splits occur
    Dim totalColumns As Integer     ' This stores the number of columns contained in the report(s)
    Dim splitName As String
    Dim tableNames() As String
    Dim outputSheetMarker As Range  ' This cycles through the output sheet(s) to find column/row names where needed
 
    ' PivotTable variables
    Dim pivotTableCaches() As PivotCache  ' Stores PT caches
    Dim pivotTableArray() As PivotTable     ' Stores PT objects
    Dim tableDataRange() As Range           ' Stores Ranges on the summary sheets where the PT data is located
 
    ' For clarity, removed this section as it only sets up variables, defines boundaries and PivotTable rows
 
    ' This section detemines the number of PvtTbls to create and sizes the arrays accordingly.  (It does so correctly.)
 
    ' PivotTables are named by the value of the category where the data split occurs (splitName), "Pivot" and the type of sheet the Table is created from.
 
    ' Now we are ready to cycle through the splitRows and create the PivotTables
    For j = 0 To (UBound(splitRows) - 1)
        ' First, we need to create the table name
        If (rptTypes = "Absolute Only" Or rptTypes = "AbsAndRel") Then
            ' Create the table name
            splitName = Sheets(outputSheet & " - Abs Date").Cells(splitRows(j) + 1, i + 1).Value
            tableNames(k) = splitName & "PivotTableAbs"
            ' Set the data range
            Set tableDataRange(k) = Sheets(outputSheet & " - Abs Date").Cells(splitRows(j), 1).Resize(splitRows(j + 1) - 1, totalColumns)
            Set pivotTableCaches(k) = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=tableDataRange(k))
 
           Set pivotTableArray(k) = pivotTableCaches(k).CreatePivotTable(TableDestination:="", tableName:=tableNames(k)) 
           ' NOTE: The line above above properly creates the PivotTable object here, but fails in the next section.
 
           ' NOTE: Removed code here adds columns and data rows to the created PivotTable
 
        ' Since the PT is created on a new, generically-named sheet, removed code here determines if the final name for the sheet matches one that already exists and deletes that sheet if so, then renames the ActiveSheet to the desired name.
 
        k = k + 1
 
        End If
 
        If (rptTypes = "Relative Only" Or rptTypes = "AbsAndRel") Then
            splitName = Sheets(outputSheet & " - Rel Date").Cells(splitRows(j) + 1, i + 1).Value
            tableNames(k) = splitName & "PivotTableRel"
            Set tableDataRange(k) = Sheets(outputSheet & " - Rel Date").Cells(splitRows(j), 1).Resize(splitRows(j + 1) - 1, totalColumns)
            Set pivotTableCaches(k) = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=tableDataRange(k))
 
[B]' ********* NOTE: ************[/B]
[B]' This is the line where the program breaks[/B]
[B]          Set pivotTableArray(k) = pivotTableCaches(k).CreatePivotTable(TableDestination:="", tableName:=tableNames(k))   [/B]
[B]' ***********************[/B]
 
' NOTE: The code here is a copy of the code from above that adds columns and data rows                        
 
' Repeat of the sheet naming procedures from the first section
 
            k = k + 1   ' Increments k to the next Cache/Table array position
        End If
 
        Next j
 
End Function

Like I said, I have tried this a thousand ways, and I do not understand why the same code works in the first section and fails in the second.

I should point out that on the first PvtTbl creation, the program is pulling data from the first section of one of the summary sheets. In the second creation (the one that fails), it is pulling data from a different sheet, though the range of data on that sheet is the same. The program will then loop through again to pull the next range of data from the first sheet and then the second sheet, and then repeat that process until the bottom of the summarized data is reached.

Hope you could follow all of this. I am willing to bet it is something very simple that I am overlooking (this is my first time coding PivotTables in VBA), but hopefully you guys can help me fix this.

Thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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