Filter for Dates which are in Caption in PivotTables which come from Olap cube

▼魔方 西西 提交于 2019-12-24 20:32:54

问题


I want to Filter Dates which stands between two Dates within a Olap Cube.

My code so far:

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

The error Message is:

Runtime Error 438: Object does not support property or method.

Manually opened plus signs (referenced in code):

Update

Ok, what i fount out so far:

With:

For Each i In .PivotItems

        Debug.Print i.Caption  'it prints e.g. Monday, September 04 2017
Next i

I cant use .IsDate on this Field or the items, so it is not a date?

If I Format it my start_date to this Format i get Montag, September 04 2017 (the german version, because i am using a german pc). Is this relevant for my problem?

Update2:

i have tried:

.PivotFilters.Add Type:=xlCaptionIsGreaterThan, Value1:=Format(start_date, "dddd, mmmm dd yyyy")  'getting here the error

and the result is, that just Wednesdays are selected with start_date = Wednesday, August 23 2017 ( i have changed my System settings from German to English)

Resulting Question--> Can i convert my PivotField to a Date?

Update3

I converted the Olap Date with the following for loop and getting now a new error:

1004:The date you entered is not a valid date. Please try again.

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

Also tried that with Cdbl in CDate(u) and start_date/end_date and getting this error message:

5: Invalid procedure call or argument.


回答1:


Fire up the macro recorder, set the DateBetween filter, then take a look at the code it recorded. Then compare that code to what you are doing above, and you'll probably be able to find where you went wrong. When I did it, I got this:

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

Note that the macro recorder didn't care that the dates were formatted in dddd, mmmm dd yyyy format...it just used the d/mm/yyyy format appropriate for my location (New Zealand). That's probably where you are going wrong.



来源:https://stackoverflow.com/questions/46151962/filter-for-dates-which-are-in-caption-in-pivottables-which-come-from-olap-cube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!