【问题标题】:Filter for Dates which are in Caption in PivotTables which come from Olap cube筛选来自 Olap 多维数据集的数据透视表中标题中的日期
【发布时间】: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.AddPivotFilters.Add2 仅适用于 Excel 2013
  • 现在.PivotFilters.Add Type:=xlCaptionIsBetween 没有任何错误,但是我的所有数据都被过滤掉了,尽管我知道这些日期之间有数据
  • 将日期更改为Double

标签: vba date filter pivot-table olap


【解决方案1】:

启动宏记录器,设置 DateBetween 过滤器,然后查看它记录的代码。然后将该代码与您在上面所做的进行比较,您可能会发现哪里出错了。当我这样做时,我得到了这个:

ActiveSheet.PivotTables("PivotTable3").PivotFields("[Range].[Date].[Date]"). _
        PivotFilters.Add2 Type:=xlDateBetween, Value1:="1/02/2017", Value2:= _
        "1/08/2017"

请注意,宏记录器并不关心日期格式是否为 dddd、mmmm dd yyyy 格式...它只是使用适合我所在位置(新西兰)的 d/mm/yyyy 格式。这可能就是你出错的地方。

【讨论】:

  • 这有帮助吗?如果是这样,请您选择我的答案吗?
猜你喜欢
  • 2013-10-10
  • 2018-09-12
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
相关资源
最近更新 更多