简体   繁体   中英

Excel-VBA: Change the Filter of only one Pivot-Table

i'm searching for a soluton for this:

I have a wookbook, in which are multiple sheets, in which are multiple pivot-tables. I simply want to change the Filter of only one specific pivot-table.

If I use the ActiveSheet-Method this seems to work but this Sheet is not active when I start this code, so I try to change this filter by adressing it:

Sub Jahresübergang2()

Dim Jahresuebergang2 As Worksheet
    Set Jahresuebergang2 = Worksheets(11)

Jahresuebergang2.PivotTables("Name of Pivot Table").PivotFields("[Incident resolved].[Period].[Year]").VisibleItemsList = Array("[Incident resolved].[Period].[Year].&[2017]")

End Sub

I get Runtime-Error 1004: Run-time error '1004': Method 'PivotTables' of object '_worksheet' failed

It's like I am doint the wrong object-syntax to change filters of only one table.

Here is what the makro recorder is doing:

Sub Makro1()
'
' Makro1 Makro
'

'
    ActiveSheet.PivotTables("Name of Pivot Table").PivotFields( _
        "[Incident resolved].[Year].[Year]").ClearAllFilters
    ActiveSheet.PivotTables("Name of Pivot Table").PivotFields( _
        "[Incident resolved].[Year].[Year]").CurrentPageName = _
        "[Incident resolved].[Year].&[2017]"
End Sub

You see: I don't want to use the activesheet method, due this sheet isn't active when I want to perform this makro.

Thanks a lot for your help!

PS: I use Excel 365

Your code has [Incident resolved]. [Period] .[Year] whereas the macro recorder has [Incident resolved]. [Year] .[Year]

Change your code to say [Incident resolved]. [Year] .[Year] and try again.

Here my Solution:

TabelleX.Activate (X for the Number of the Sheet)

to activate the sheet. The only problem may be that this sheet will be active on the monitor during the makro-process. I solved this with:

Application.ScreenUpdating = False (Code) Application.ScreenUpdating = True

so activation won't be updated on the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM