遇到此问题的问题: 我想循环遍历VBA 2010中的数据透视表日期字段,以便代替编写:
ActiveSheet.PivotTables("MyPivotTable").PivotFields( _
"[Dim Time].[Year Month Day].[Date YYYYMMDD]").VisibleItemsList = Array( _
"[Dim Time].[Year Month Day].[Year].&[2015].&[01].&[2015/01/01]", _
"[Dim Time].[Year Month Day].[Year].&[2015].&[01].&[2015/01/02]", _
"And so on......... until"
"[Dim Time].[Year Month Day].[Year].&[2015].&[01].&[2015/01/19]")`
我会写类似
的内容Dim Mnths As Integer
Dim Days As Integer
For Mnths = 1 To 5
For Days = 1 To 19
Sheets("MtShhet").Select
ActiveSheet.PivotTables("MyPivotTable").PivotFields( _
"[Dim Time].[Year Month Day].[Date YYYYMMDD]").VisibleItemsList = Array( _
"[Dim Time].[Year Month Day].[Year].&[2015].&[" & Mnths & "].&[2015/" & Mnths & "/" & Days & "])"
Next Days
Next Mnths
我环顾四周,但是我看到的大部分解释都显示了不同形式的日期而不是本例中的数组。这个奇怪的表达式作为我在录制宏时获得的数组的日期。我想它与数据透视表创建者(不是我)有关。
答案 0 :(得分:0)
我无法真正测试最后一部分,但你可以像这样构建数组:
Dim Mnths As Integer
Dim Days As Integer
Dim vDates(1 To 95)
Const csDIMENSION As String = "[Dim Time].[Year Month Day].[Year].&[2015].&["
For Mnths = 1 To 5
For Days = 1 To 19
vDates((Mnths - 1) * 19 + Days) = csDIMENSION & Mnths & "].&[2015/" & Format$(Mnths, "00") & "/" & Format$(Days, "00") & "])"
Next Days
Next Mnths
Sheets("MtShhet").PivotTables("MyPivotTable").PivotFields("[Dim Time].[Year Month Day].[Date YYYYMMDD]").VisibleItemsList = vDates