Help with code to remove Sum of from Pivot headers

Serafin54

Board Regular
Joined
Apr 11, 2014
Messages
155
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a code that does remove Sum of from the header but only "Sum of" I can't for the life of me figure out what i'm missing to also remove the space that follows "of" I messed with the pf.Cation 6 and -6 as well as adding a space in the quotes as i do. I can remove less or more but cannot remove that one space. any help is greatly appreciated to point me in the right direction. Work would frown should i punch a monitor. lol

Code:
Sub Trim_Header()
Dim pt As PivotTable, pf As PivotField, ws As Worksheet, i As Long
Application.ScreenUpdating = False
Set ws = ActiveSheet
For i = 1 To ws.PivotTables.Count
Set pt = ws.PivotTables(i)
pt.ManualUpdate = True
For Each pf In pt.DataFields
If pf.Function = xlSum Then
If Left(pf.Caption, 6) = "Sum of" Then
pf.Caption = Right(pf.Caption, Len(pf.Caption) - 6)
End If
End If
Next
pt.ManualUpdate = False
Next i
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
A pivot table won't let you have an aggregation or calculated column that has exactly the same name as a column heading in the underlying data.

The work around is usually to "add a space" to the field name.
By you trying to remove that space from the pivot field name you are making it exactly the same as underlying data field name.

The only way to remove that leading space is to change something else in the field name eg by adding a trailing space while removing the leading space.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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