dynamic update pivot chart title

joecast

Board Regular
Joined
Mar 4, 2010
Messages
52
hello all,

i have set up a macro to validate which pivot chart fields are selected and edit text in the chart title based on that. so the idea is the user can set the filters within the chart and see which regions are selected in the title.

this code will step through ok but if run on its own excel will crash.

the pivot table event runs from the pivot chart which is on a different sheet to the pivot table. any suggestions for what might be the problem? thanks,


Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)    
    Dim wsSewer, wsCharts As Worksheet
    Dim chtTitle As String
    Dim north, northwest, south As Boolean
    
    Set wsSewer = ActiveWorkbook.Worksheets("sewer block choke pt")
    Set wsCharts = ActiveWorkbook.Worksheets("Charts")
    
    north = wsSewer.PivotTables("sewerPT").PivotFields("Region Name").PivotItems("North").Visible
    northwest = wsSewer.PivotTables("sewerPT").PivotFields("Region Name").PivotItems("NorthWest").Visible
    south = wsSewer.PivotTables("sewerPT").PivotFields("Region Name").PivotItems("South").Visible
    
    wsCharts.ChartObjects("sewer pc").Activate
       
    If north Then
        
        If northwest Then
        
            If south Then
                chtTitle = "All"
            Else
                chtTitle = "North & North West"
            End If
        Else
            If south Then
                chtTitle = "North & South"
            Else
                chtTitle = "North"
            End If
        End If
        
    Else
        If northwest Then
        
            If south Then
                chtTitle = "North West & South"
            Else
                chtTitle = "North West"
            End If
        Else
            If south Then chtTitle = "South"
            'End If
        End If
    End If
    
    With wsCharts.ChartObjects("sewer pc")
        .Chart.HasTitle = True
        .Chart.ChartTitle.Text = chtTitle
    End With


End Sub
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

joecast

Board Regular
Joined
Mar 4, 2010
Messages
52
hmmm. still causing a problem. now crashes when stepping through too. seems to get to the end of the code so will update the chart title but will crash after end sub call.
 

joecast

Board Regular
Joined
Mar 4, 2010
Messages
52
For the early crowd. Might disable this for now until I can work out a solution. Will post back with an update.
 

joecast

Board Regular
Joined
Mar 4, 2010
Messages
52
Seems Ive solved the problem. Had to do with which sheet was active when the chart title edit was happening. Added a line for sheet select and now works fine :)
 

Watch MrExcel Video

Forum statistics

Threads
1,109,523
Messages
5,529,334
Members
409,863
Latest member
stacy09
Top