使用 VBA 在 Visibleitemslist 中过滤年份和月份以获得 OLAP 数据透视表 table
Filtering both year and month in Visibleitemslist for an OLAP pivot table using VBA
我一直在尝试自动化这个根据输入日期字段过滤日期范围的宏(我已将输入日期指定为 2022 年 7 月)。此日期范围过滤器的应用方式应包括输入年份之前的三年(即 2020-2022)和输入月份之前的所有月份(一月至七月)。
我不确定如何为下面的过滤器创建一个通用的 for 循环,它会根据输入字段自动更改年份和月份。我试图为当年的月份创建一个 for 循环,但不知道如何组合前三年的范围。
Sub Macro2()
'
' Macro2 Macro
'
'
ActiveSheet.PivotTables("NetRate22").PivotFields( _
"[Policy State Dimensions].[Effective Date].[Effective Month]"). _
VisibleItemsList = Array( _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[7]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[7]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[7]")
End Sub
一个月来我一直在努力解决这个问题,但在 VBA 中没有找到最好的资源来帮助解决这个问题。
对此问题的任何帮助表示赞赏。谢谢!
未测试。
Sub Macro2()
' visible item list
Const VIL = "[Policy State Dimensions].[Effective Date].[Effective Month]"
Const YRS = 3 ' prior years
Dim dt As Date
dt = "1/7/2022" ' July 2022
Dim mth As Long, yr As Long
Dim m As Long, y As Long, i As Long
Dim ar
mth = Month(dt)
yr = Year(dt)
ReDim ar(0 To mth * YRS - 1)
' build array
i = 0
For y = YRS To 1 Step -1
For m = 1 To mth
ar(i) = VIL & ".&[" & (yr - y + 1) & "]&[" & m & "]"
i = i + 1
Next
Next
'Debug.Print Join(ar, vbLf)
ActiveSheet.PivotTables("NetRate22").PivotFields(VIL).VisibleItemsList = ar
End Sub
我一直在尝试自动化这个根据输入日期字段过滤日期范围的宏(我已将输入日期指定为 2022 年 7 月)。此日期范围过滤器的应用方式应包括输入年份之前的三年(即 2020-2022)和输入月份之前的所有月份(一月至七月)。
我不确定如何为下面的过滤器创建一个通用的 for 循环,它会根据输入字段自动更改年份和月份。我试图为当年的月份创建一个 for 循环,但不知道如何组合前三年的范围。
Sub Macro2()
'
' Macro2 Macro
'
'
ActiveSheet.PivotTables("NetRate22").PivotFields( _
"[Policy State Dimensions].[Effective Date].[Effective Month]"). _
VisibleItemsList = Array( _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2020]&[7]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2021]&[7]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[1]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[2]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[3]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[4]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[5]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[6]", _
"[Policy State Dimensions].[Effective Date].[Effective Month].&[2022]&[7]")
End Sub
一个月来我一直在努力解决这个问题,但在 VBA 中没有找到最好的资源来帮助解决这个问题。 对此问题的任何帮助表示赞赏。谢谢!
未测试。
Sub Macro2()
' visible item list
Const VIL = "[Policy State Dimensions].[Effective Date].[Effective Month]"
Const YRS = 3 ' prior years
Dim dt As Date
dt = "1/7/2022" ' July 2022
Dim mth As Long, yr As Long
Dim m As Long, y As Long, i As Long
Dim ar
mth = Month(dt)
yr = Year(dt)
ReDim ar(0 To mth * YRS - 1)
' build array
i = 0
For y = YRS To 1 Step -1
For m = 1 To mth
ar(i) = VIL & ".&[" & (yr - y + 1) & "]&[" & m & "]"
i = i + 1
Next
Next
'Debug.Print Join(ar, vbLf)
ActiveSheet.PivotTables("NetRate22").PivotFields(VIL).VisibleItemsList = ar
End Sub