VBA Test If Chart Is A Pivot Chart

Chris Macro

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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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!
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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