Attempting VBA to refresh "Chart Title" based on Pivot Filters. Filter1 works in VBA, but title doesn't update based on filter2.

ghull99

New Member
Joined
Oct 22, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
The following is my VBA Code:

Sub UpdateChartTitle()
Dim pt As PivotTable
Dim pf As PivotField
Dim ws As Worksheet
Dim chartTitle As String
Dim filter1 As String
Dim filter2 As String

' Set the worksheet and pivot table
Set ws = ThisWorkbook.Sheets("Top Overall & EU Complaints") ' Change to your sheet name
Set pt = ws.PivotTables("PivotTable34") ' Change to your pivot table name

' Loop through each pivot field in the pivot table
For Each pf In pt.PivotFields
If pf.Orientation = xlPageField Then
If pf.Name = "Region" Then ' Change to your first filter field name
Debug.Print "Region: [" & pf.CurrentPage & "]" ' Debugging line
If Trim(pf.CurrentPage) = "(All)" Then
filter1 = "Worldwide" ' Replace with "Worldwide"
Else
filter1 = pf.CurrentPage
End If
Debug.Print "Filter1 Result: " & filter1 ' Debugging line
ElseIf pf.Name = "Name" Then ' Change to your second filter field name
Debug.Print "Name: [" & pf.CurrentPage & "]" ' Debugging line
If Trim(pf.CurrentPage) = "(All)" Then
filter2 = "All Products" ' Replace with "All Products"
Else
filter2 = pf.CurrentPage
End If
End If
End If
Next pf

' Combine filters into the chart title
chartTitle = filter1 & " Top Complaint Types for " & filter2

'Set the chart title
ws.ChartObjects("Chart 3").chart.chartTitle.text = chartTitle 'Change to your chart name
End Sub
Sub RefreshChartTitle()
' Call the UpdateChartTitle subroutine whenever the pivot table is updated
Call UpdateChartTitle
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" Then
MsgBox ("Region Has Changed.")
End If
If Target.Address = "$C$5" Then
MsgBox ("Product Name Has Changed.")
End If
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,223,869
Messages
6,175,087
Members
452,611
Latest member
bls2024

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