Pivot Table Sourcedata

sbhat

New Member
Joined
Apr 8, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Sub PivotID()
Dim wb As Workbook
Dim sh As Worksheet
Dim pt As PivotTable

Set wb = ActiveWorkbook

For Each sh In wb.Worksheets
MsgBox sh.Name & " has " & sh.PivotTables.Count & " pivot tables"
If sh.PivotTables.Count > 0 Then
For Each pt In sh.PivotTables
MsgBox pt.Name & " source data is " & pt.SourceData
Next
End If


Next

End Sub

===========
The above code works in normal course when pivot tables are based on cell-ranges; however, when a pivot table is based on data model the pt.SourceData throws error 1004: object not defined. What is the remedy?
 

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.
Hi sbhat,

The 'SourceData' property of a PivotTable object returns the cell range that the PivotTable is based on. However, when a PivotTable is based on a data model, the 'SourceData' property is not applicable and will return an error.

To obtain the source data for a PivotTable that is based on a data model, you can use the 'DataBodyRange' property of the 'ListObject' object that represents the data model table.

Try using this code:
VBA Code:
Sub PivotID()
    Dim wb As Workbook
    Dim sh As Worksheet
    Dim pt As PivotTable

    Set wb = ActiveWorkbook

    For Each sh In wb.Worksheets
        MsgBox sh.Name & " has " & sh.PivotTables.Count & " pivot tables"
        If sh.PivotTables.Count > 0 Then
            For Each pt In sh.PivotTables
                On Error Resume Next
                MsgBox pt.Name & " source data is " & pt.SourceData
                If Err.Number = 1004 Then
                    Err.Clear
                    MsgBox pt.Name & " source data is " & pt.TableRange1.ListObject.DataBodyRange.Address
                End If
                On Error GoTo 0
            Next pt
        End If
    Next sh
End Sub

This code is for when the PivotTable is based on a single data model table.
Let me know if this works.


Best Regards.
 
Last edited by a moderator:
Upvote 0
Hi sbhat,

The 'SourceData' property of a PivotTable object returns the cell range that the PivotTable is based on. However, when a PivotTable is based on a data model, the 'SourceData' property is not applicable and will return an error.

To obtain the source data for a PivotTable that is based on a data model, you can use the 'DataBodyRange' property of the 'ListObject' object that represents the data model table.

Try using this code:
VBA Code:
Sub PivotID()
    Dim wb As Workbook
    Dim sh As Worksheet
    Dim pt As PivotTable

    Set wb = ActiveWorkbook

    For Each sh In wb.Worksheets
        MsgBox sh.Name & " has " & sh.PivotTables.Count & " pivot tables"
        If sh.PivotTables.Count > 0 Then
            For Each pt In sh.PivotTables
                On Error Resume Next
                MsgBox pt.Name & " source data is " & pt.SourceData
                If Err.Number = 1004 Then
                    Err.Clear
                    MsgBox pt.Name & " source data is " & pt.TableRange1.ListObject.DataBodyRange.Address
                End If
                On Error GoTo 0
            Next pt
        End If
    Next sh
End Sub

This code is for when the PivotTable is based on a single data model table.
Let me know if this works.


Best Regards.
Chris: Thank you for addressing my query. I executed the VBA script you provided; as I also had thought and executed yesterday, I used "On Error Resume Next" piece of code to circumvent the error-prone pt.SourceData (when the pivot is based on data model). I got the result without any error. However, the line "
MsgBox pt.Name & " source data is " & pt.TableRange1.ListObject.DataBodyRange.Address is not found executed; otherwise, I still didn't see the data source of those pivots which are based on data model. The pivots based on plain data range (not the data model ones) gave proper source data info.
 
Upvote 0
What information do you actually want returned if it's a data model pivot? Just the words "data model" or something else?
 
Upvote 0
If you have multiple pivot's then you could give the code on the contextures site a try, since it will list all the sources on a separate sheet:
The link should take you straight to the section:
List Pivot Table Data Source or MDX

The key part of the code being:
VBA Code:
   If pt.PivotCache.OLAP = False Then
      strSource = pt.SourceData
      strMDX = ""
    Else
      strSource = "OLAP"
      strMDX = pt.MDX
    End If
 
Upvote 0
What information do you actually want returned if it's a data model pivot? Just the words "data model" or something else?
The Data Model would be having a "Range" tab which is pointing to a worksheet's cell range in turn. Therefore, source data actually is a worksheet's cell range which is the "Range" for the data model.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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