VBA Test If Chart Is A Pivot Chart

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,341
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).
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
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,341
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:

Watch MrExcel Video

Forum statistics

Threads
1,122,840
Messages
5,598,386
Members
414,234
Latest member
grlevesq

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