VBA Test If Chart Is A Pivot Chart

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,343
Office Version
  1. 365
Platform
  1. Windows
Does anyone have a good way to test if a chart is a Pivot Chart in VBA? I have code that is looping through all the chart objects that are currently selected but want to ensure none of them are Pivot Charts (this is more of an error handler than anything else).
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,224
Office Version
  1. 365
Platform
  1. Windows
Maybe...

Code:
Sub test()
    Dim oChrtObj As ChartObject
    Dim oPivotLayout As PivotLayout
    
    For Each oChrtObj In ActiveSheet.ChartObjects
        Set oPivotLayout = oChrtObj.Chart.PivotLayout
        If oPivotLayout Is Nothing Then
            MsgBox oChrtObj.Name & " is not a Pivot Chart."
        Else
            MsgBox oChrtObj.Name & " is a Pivot Chart."
        End If
    Next oChrtObj
End Sub

Hope this helps!
 

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,343
Office Version
  1. 365
Platform
  1. Windows
Thank you Domenic! That got me exactly what I needed.

Ended up making a little function since I needed to test for Pivot Charts across a few modules in my VBA project.

Code:
Private Function IsPivotChart(cht As Chart) As Boolean
'PURPOSE: Determine whether a chart is a Pivot Chart

Dim pvtLayout As PivotLayout
    
Set pvtLayout = cht.PivotLayout

If pvtLayout Is Nothing Then
  IsPivotChart = False
Else
  IsPivotChart = True
End If

End Function
 
Last edited:

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,224
Office Version
  1. 365
Platform
  1. Windows
That's great. Glad I could help.

Cheers!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,025
Messages
5,835,016
Members
430,332
Latest member
Charly_Moon

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
Top