【发布时间】:2018-02-19 11:23:34
【问题描述】:
我想过滤位于 Olap Cube 中两个日期之间的日期。
到目前为止我的代码:
Set pvtField = pt.CubeFields("[DimTime].[Year-Quarter-Month-Day]")
' Add item to the Report Filter
pvtField.CreatePivotFields
pvtField.Orientation = xlRowField
With ActiveSheet.PivotTables(1).PivotFields("[DimTime].[Year-Quarter-Month-Day].[Day]")
Debug.Print end_date 'return Date in the format 'dd.mm.yyyy', it is a valid Date
Debug.Print start_date 'return Date in the format 'dd.mm.yyyy', it is a valid Date
Debug.Print .PivotItems(1).Caption 'returns e.g. Monday, September 04 2006, this is just returned, if i click on the plus sign in the pivot table(see attached picture)
Debug.Print .PivotItems(1).Value 'returns e.g. [DimTime].[Year-Quarter-Month-Day].[Day].&[2006-09-04T00:00:00]
Debug.Print .Name 'returns e.g. [DimTime].[Year-Quarter-Month-Day].[Day]
.ClearAllFilters 'Clear All The Filters
.CubeField.IncludeNewItemsInFilter = True
.PivotFilters.Add2 Type:=xlCaptionIsBetween, Value1:=start_date, Value2:=end_date 'getting here the error
'.PivotFilters.Add2 Type:=xlDateBetween, Value1:=start_date, Value2:=end_date 'getting here the same error
End With
错误信息是:
运行时错误 438:对象不支持属性或方法。
手动打开的加号(代码中引用):
更新
好的,到目前为止我发现了什么:
与:
For Each i In .PivotItems
Debug.Print i.Caption 'it prints e.g. Monday, September 04 2017
Next i
我不能在此字段或项目上使用 .IsDate,所以它不是日期?
如果我将它的 start_date 格式化为这种格式,我会得到 Montag,2017 年 9 月 4 日(德文版,因为我使用的是德文电脑)。这与我的问题有关吗?
更新2:
我试过了:
.PivotFilters.Add Type:=xlCaptionIsGreaterThan, Value1:=Format(start_date, "dddd, mmmm dd yyyy") 'getting here the error
结果是,只选择了周三 start_date = 周三,2017 年 8 月 23 日(我已将系统设置从德语更改为英语)
结果问题-->我可以将我的数据透视字段转换为日期吗?
更新3
我使用以下 for 循环转换了 Olap Date,现在出现了一个新错误:
1004:您输入的日期不是有效日期。请重试。
For Each i In .PivotItems
Debug.Print i.Caption
'Debug.Print CDate(CStr(i.Caption))
p = i.Caption
u = Split(p, ",")(1)
Debug.Print CDate(u) 'e.g. 04/09/2017
i.Caption = CDate(u)
Next i
'Debug.Print .PivotItems(1).IsDate
.PivotFilters.Add Type:=xlDateBetween, Value1:=start_date, Value2:=end_date 'getting here the error
还尝试在 CDate(u) 和 start_date/end_date 中使用 Cdbl 并收到此错误消息:
5: 无效的过程调用或参数。
【问题讨论】:
-
您使用的是 Excel 2013(或更高版本)吗?
-
@ShaiRado 不,我使用的是 Excel 2010。
-
那么您需要使用
PivotFilters.Add,PivotFilters.Add2仅适用于 Excel 2013 -
现在.PivotFilters.Add Type:=xlCaptionIsBetween 没有任何错误,但是我的所有数据都被过滤掉了,尽管我知道这些日期之间有数据
-
将日期更改为
Double
标签: vba date filter pivot-table olap