VBA To make pivotfield use its source datas format

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
Hi,
As the title says I am trying to have my pivot table use the format for each field that its source data uses.

I have used this code to change the names of each of the pivotfields in the pivot table and would preferably like a way to do this using the for loop indicated below when changing the names. Is there anyway to throw this in? Thanks

Dim pt As PivotTable
Dim pf As PivotField

'some of the commands may cause VBA errors in some situations
'- these will be ignored as they are not important
On Error Resume Next

'capture the selected pivot table
Set pt = ActiveSheet.PivotTables("SummaryReport")

For Each pf In pt.DataFields
'add a space to the data field name because Excel won't
'allow the use of a data field name within the pivot table
pf.Caption = pf.SourceName & " "
Next pf

'reset error handling
On Error GoTo 0

'clear object variables
Set pt = Nothing
Set pf = Nothing
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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