2016-08-17 46 views
0

過濾透視表我有主從,我有每天的數據透視表中信息的圖表。 我正在嘗試創建activeX按鈕,以便它可以過濾作爲行標籤的數據,以查看我的數據在上個星期和上個月的行爲如何使用VBA

因此,我到目前爲止所擁有的並不是工作是:

私人小組weekbtn1_Click() 暗淡我作爲整數

If weekbtn1 = True Then 
i = 0 
Do Until Datavalue(date) - i = 42005 
With ActiveSheet.PivotTables("Pivotcompsprice").PivotFields("Date") 
    .PivotItems DateValue(Date) - i.Visible = False 
    i = i + 1 
End With  
Loop 

i = 0 
Do Until i = 7 
With ActiveSheet.PivotTables("Pivotcompsprice").PivotFields("Date") 
    .PivotItems Datevalue(date) - i.Visible = True 
End With 
Loop 
Else 
End If 

末次

我把這個42005,因爲它是我有1/1是數據的最後日期/2015...我是認爲這是可以過濾所有的數據爲「假的」,然後弄真我想要的東西,但它不工作!

有人可以幫助我嗎?

+0

ps:我把vba放在上週......我的意圖是對上個月做一些修改的宏 –

回答

0

這是不可能隱藏在透視字段的所有項目。你總是必須離開至少一個可見的。

如果使用VBA利用內置的日期過濾器功能即這將是更快,更容易: DateFilter

下面是一個示例文件,我做到這一點: https://1drv.ms/x/s!Ah_zTnaUo4DzjhezQ3OTq9tq1APC

注這個功能僅在RowFields或ColumnFields可用。所以我的代碼在PageFields上不起作用。

這裏有一個通用的程序,讓您選擇要在篩選透視字段,以及選擇性地選擇你要計算從前進/後退的日期間隔是什麼類型和間隔時間。

Sub Pivots_FilterPeriod(sInterval As String, _ 
         lNumber As Long, _ 
         Optional vRelativeTo As Variant, _ 
         Optional pf As PivotField) 

    'Programmer:  Jeff Weir 
    'Contact:   [email protected] 
    'Description:  Lets you programatically filter a Pivot RowField or ColumnField by specifying 
    '     an interval type (e.g. days, weeks, months, quarters, years) 
    '     as well as an interval count (e.g. 7, -7) 
    '     If the optional vRelativeTo field is left blank, it counts back/foward from 
    '     the youngest/oldest item depending on whether lNumber is positive/negative 
    '     It leverages off the inbuilt DateFilters functionality, and as such does not 
    '     work on RowFields. 


    Dim dteDateAdd As Date 
    Dim dteFrom As Date 
    Dim dteTo As Date 

    On Error GoTo errhandler 

    If pf Is Nothing Then 
     On Error Resume Next 
     Set pf = ActiveCell.PivotField 
     On Error GoTo errhandler 
     If pf Is Nothing Then GoTo errhandler 
    End If 

    With pf 
     If .DataType = xlDate _ 
      And .Orientation <> xlPageField _ 
      And .Orientation <> xlDataField Then 

      If IsMissing(vRelativeTo) Or vRelativeTo = "" Then 
       .AutoSort xlAscending, "Date" 
       If lNumber > 0 Then 
        vRelativeTo = .PivotItems(1) 
       Else 
        vRelativeTo = .PivotItems(.PivotItems.Count) 
       End If 
      End If 

      Select Case UCase(sInterval) 
       Case "D", "DD", "DDD", "DDDD", "DAY", "DAYS": sInterval = "d" 
       Case "W", "WW", "WWW", "WWWW", "WEEK", "WEEKS": sInterval = "ww" 
       Case "M", "MM", "MMM", "MMMM", "MONTH", "MONTHS": sInterval = "m" 
       Case "Q", "QQ", "QQQ", "QQQQ", "QUARTER", "QUARTERS": sInterval = "q" 
       Case "Y", "YY", "YYY", "YYYY", "YEAR", "YEARS": sInterval = "yyyy" 
      End Select 

      dteDateAdd = DateAdd(sInterval, lNumber, vRelativeTo) 
      If lNumber > 0 Then 
       dteDateAdd = dteDateAdd - 1 
      Else 
       dteDateAdd = dteDateAdd + 1 
      End If 

      If dteDateAdd < vRelativeTo Then 
       dteFrom = dteDateAdd 
       dteTo = vRelativeTo 
      Else 
       dteFrom = vRelativeTo 
       dteTo = dteDateAdd 
      End If 

      With Application 
       .ScreenUpdating = False 
       .EnableEvents = False 
       .Calculation = xlCalculationManual 
      End With 

      .ClearAllFilters 
      .PivotFilters.Add2 _ 
       Type:=xlDateBetween, _ 
       Value1:=CStr(dteFrom), _ 
       Value2:=CStr(dteTo) 
     End If 
    End With 

errhandler: 
    With Application 
     .ScreenUpdating = True 
     .EnableEvents = True 
     .Calculation = xlCalculationAutomatic 
    End With 

End Sub 

下面是一些截圖,演示了它在使用不同參數時的外觀。

這說明如何在最近5天過濾從最近的數據:

Last 5 days from most recent data

,並通過更改標誌,它的作品了,我們必須要從最早的數據,第5天記錄:

First 5 days from oldest records

如果指定在該領域對於relativeTo的實際日期,它會計算前/後從那裏取決於數參數是否爲正/ negat我有。下面是今天的日期,未來5天,因爲我寫這篇文章:

Next 5 days from today

...這是最近5天:

enter image description here

它可以讓你指定你是否想天,數週,數季,數月或數年。舉例來說,這裏是過去兩週從最近的記錄計數回:

enter image description here

我使用Worksheet_Change事件這裏來觸發它,但如果你喜歡,你可以把它掛到一個按鈕,並給它提供你想要的參數。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim bContinue As Boolean 
    If Not Intersect(Target, Range("Interval")) Is Nothing Then bContinue = True 
    If Not Intersect(Target, Range("Number")) Is Nothing Then bContinue = True 
    If Not Intersect(Target, Range("RelativeTo")) Is Nothing Then bContinue = True 
    If bContinue Then Pivots_FilterPeriod [Interval], [Number], [RelativeTo], Sheet1.PivotTables(1).PivotFields("Date") 

End Sub