Refresh row headers in pivot tables with vba

__ndt__

New Member
Joined
Apr 8, 2015
Messages
2
Hello,

I need to refresh (i.e. in this case: use the SourceName again) row headers in pivot tables with vba.

I found code (probably written with a former version af Excel):

Code:
Public Sub piv_kor()
Dim wS As Worksheet
Dim pT As PivotTable
Dim pF As PivotField
Dim pI As PivotItem
 
For Each wS In ActiveWorkbook.Worksheets
    For Each pT In wS.PivotTables
    pT.PivotCache.Refresh
        For Each pF In pT.PivotFields
            For Each pI In pF.PivotItems
                pI.Name = pI.SourceName
            Next pI
        Next pF
    Next pT
Next wS
End Sub


I get a runtime error (Type mismatch (Error 13). As I understand it from the Microsoft documentation, pI.Name is a string, whereas pI.SourceName is a variant. If I use pI.SourceNameStandard (which is also a string), the code works fine, but i get american date formats (some of my row headers are dates, and I need them to stay in the format of my region DD.MM.YYYY).

I tried to convert the variant to string using the following code, but I then get a runtime error (Invalid procedure call or argument (Error 5):

Code:
...
pI.Name = Cstr(pI.SourceName)
...

I guess I am doing something wrong with the conversion. Any help would be much appreciated!

Andreas

---
I am using Excel 2007 on a Terminal Server (Microsoft Windows Server 2008 R2 Enterprise). I am new to vba, though I do programming in SQL and statistical software packages (SPSS, Stata), so I have an OK grasp of programming concepts.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,223,723
Messages
6,174,111
Members
452,544
Latest member
aush

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