简体   繁体   English

Excel-VBA:更改仅一个数据透视表的筛选器

[英]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. 我有一本wookbook,其中有多个工作表,其中有多个数据透视表。 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: 如果我使用ActiveSheet-Method似乎可以正常工作,但是在启动此代码时此工作表不处于活动状态,因此我尝试通过修改它来更改此过滤器:

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 我收到运行时错误1004:运行时错误'1004':对象'_worksheet'的方法'PivotTables'失败

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: 这是Makro记录器正在执行的操作:

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. 您会看到:我不想使用activesheet方法,因为当我要执行此Makro时,此工作表不处于活动状态。

Thanks a lot for your help! 非常感谢你的帮助!

PS: I use Excel 365 PS:我使用Excel 365

Your code has [Incident resolved]. 您的代码具有[事件已解决]。 [Period] .[Year] whereas the macro recorder has [Incident resolved]. [Period] 。[Year],而宏记录器具有[Incidentsolve]。 [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) TabelleX.Activate(X为工作表编号)

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 Application.ScreenUpdating = False(代码)A​​pplication.ScreenUpdating = True

so activation won't be updated on the screen. 因此激活不会在屏幕上更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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