简体   繁体   English

尝试使用VBA(Excel)更改多个数据透视表筛选器时出现错误424

[英]Error 424 when attempting to change multiple pivot table filters with VBA (Excel)

I'm writing a code to automatically change 3 filters on multiple pivot tables simultaneously upon pressing a button. 我正在编写代码,以在按下按钮时同时自动更改多个数据透视表上的3个过滤器。 There are two worksheets, one named MASTER that holds the button and 3 drop-down boxes for specifying a Year, Month, and Week, the other named PIVOTS which, obviously, holds all the pivot tables (and nothing else). 有两个工作表,一个名为MASTER的按钮,以及三个用于指定年,月和周的下拉框,另一个名为PIVOTS的表,其中显然包含所有数据透视表。

The code I've written here works EXCEPT when I have "All" selected for the Week criteria. 当我为“星期”条件选择“全部”时,此处编写的代码可以正常工作。 Our SQL cube formats weeks in the following format: YYYY-MM-DDT00:00:00. 我们的SQL多维数据集使用以下格式来格式化星期:YYYY-MM-DDT00:00:00。 I have a helper formula that checks whether or not what's chosen for Week is "All" or a date; 我有一个帮助程序公式,用于检查“星期”选择的是“全部”还是日期; if "All" is chosen, it adds nothing, but if a date is chosen, it will add "T00:00:00" to the end of the date to create the correct format. 如果选择“全部”,则不添加任何内容,但是如果选择日期,则将在日期末尾添加“ T00:00:00”以创建正确的格式。 When a date is chosen, the macro works, but when All is chosen, I get a "Run-time error '424': Object Required." 当选择一个日期时,宏起作用,但是当选择“全部”时,出现“运行时错误'424':必需对象”。

Here is the code: 这是代码:

Sub ChangeCubesMonth()
 Dim PT As PivotTable
 Dim rWeek As Range
 Dim rMonth As Range
 Dim rYear As Range
 Dim sWeek As String
 Dim sMonth As String
 Dim sYear As String


'Define ranges
Set rWeek = ThisWorkbook.Sheets("MASTER").Range("Y18")
Set rMonth = ThisWorkbook.Sheets("MASTER").Range("Y2")
Set rYear = ThisWorkbook.Sheets("MASTER").Range("E1")

'Define strings
sWeek = "[Time Date].[Workforce Week].&[" & rWeek & "]"
sMonth = "[Time Date].[Month].&[" & rMonth & "]"
sYear = "[Time Date].[Year].&[" & rYear & "]"

On Error GoTo errHandler
For Each PT In Sheets("PIVOTS").PivotTables
    With PT.PivotFields("[Time Date].[Workforce Week].[Workforce Week]")
        .ClearAllFilters
        .CurrentPageName = sWeek
    End With

    With PT.PivotFields("[Time Date].[Month].[Month]")
        .CurrentPageName = sMonth
    End With

    With PT.PivotFields("[Time Date].[Year].[Year]")
        .CurrentPageName = sYear
    End With
Next PT
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"

End Sub

When I have a date chosen, this all works perfectly. 当我选择一个日期时,这一切都很好。 However, when I have "All" chosen and use Step Into (F8), I get all the way down to the "End With" right after ".CurrentPageName = sWeek" before it goes to the error handler. 但是,当我选择“全部”并使用单步执行(F8)时,在“ .CurrentPageName = sWeek”之后,我将一直处理到“结束于”,然后再转到错误处理程序。

I've made sure "All" is the correct word to use in the VBA code to set the filter correctly, and at this point I'm stumped as to why it only doesn't work when All is selected. 我已经确保“全部”是在VBA代码中正确设置过滤器所用的正确单词,并且在这一点上,我很困惑为什么只有在选择“全部”时它才不起作用。 Any help would be appreciated! 任何帮助,将不胜感激!

Found the issue...It didn't like the & symbol before the last set of brackets when All is selected. 发现了问题...当选择全部时,它不喜欢最后一组括号前的&符号。 I added this bit of code: 我添加了以下代码:

'Define strings
If rWeek = "All" Then
    sWeek = "[Time Date].[Workforce Week].[All]"
Else
    sWeek = "[Time Date].[Workforce Week].&[" & rWeek & "]"
End If

And now the code works fine. 现在代码可以正常工作了。

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

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