我想过滤一个Olap立方体中介于两个日期之间的日期。
到目前为止我的代码是:
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: Object不支持属性或方法。
手动打开加上符号(代码中引用):
更新
好吧,到目前为止我发现的是:
通过以下方式:
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日(德文版本,因为我使用的是一台德国pc)。这跟我的问题有关吗?
Update2:
我试过:
.PivotFilters.Add Type:=xlCaptionIsGreaterThan, Value1:=Format(start_date, "dddd, mmmm dd yyyy") 'getting here the error
结果是,只选择星期三,start_date =2017年8月23日星期三(我已将系统设置从德文改为英文)。
由此产生的问题->我可以把我的PivotField转换成一个日期吗?
Update3
我用下面的for循环转换了Olap日期,现在得到了一个新的错误:
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:无效的过程调用或论证。
发布于 2017-09-13 09:00:18
启动宏记录器,设置DateBetween过滤器,然后查看它记录的代码。然后将该代码与上面所做的进行比较,您可能会发现您的错误所在。当我这么做的时候,我得到了这个:
ActiveSheet.PivotTables("PivotTable3").PivotFields("[Range].[Date].[Date]"). _
PivotFilters.Add2 Type:=xlDateBetween, Value1:="1/02/2017", Value2:= _
"1/08/2017"
注意,宏记录器并不关心日期是用dddd格式化的,mmmm format...it只是使用了适合我的位置的d/mm/yyyy格式(新西兰)。那可能就是你要出错的地方。
https://stackoverflow.com/questions/46151962
复制相似问题