2017-07-24 131 views
0

我有一張工作表,用戶可以從下拉列表中選擇多個字段用於按行標籤分組。當他們選擇日期字段時,我想按月份和年份分組。但是,我收到以下代碼的錯誤。該錯誤是Excel VBA按活動頁上所有數據透視表按月份和年份分組

運行時錯誤 '1004':範圍類的 組方法失敗

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim sField As String, sFieldName As String 
Dim oRF As Object 
Dim pPT As PivotTable 
Dim pPF As PivotField 

If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub 
If Target.Address = "$F$3" Then 
    Application.EnableEvents = False 
    sField = Target.Value 
    sFieldName = Target.Offset(0, 1).Value 



    For Each pPT In ActiveSheet.PivotTables 
     'Remove all pivot fields for all pivot tables in this sheet 
     For Each oRF In pPT.RowFields 
      Debug.Print oRF.Name 
      oRF.Orientation = xlHidden 
     Next 

     'Add the new pivot field 
     pPT.PivotFields(sField).Orientation = xlRowField 
     pPT.PivotFields(sField).Position = 1 
     pPT.PivotCache.Refresh 
     pPT.CompactLayoutRowHeader = sFieldName 

     'If the pivot field is a date field, group by month and year 
     If InStr(UCase(sFieldName), "DATE") > 0 Then 
      'set range of dates to be grouped 
      Set pPF = pPT.PivotFields(sField) 

      'This next line causes the error 
      pPT.PivotFields(sField).LabelRange.Group _ 
      Start:=True, End:=True, Periods:= _ 
       Array(False, False, False, False, True, True, True) 

     End If 
    Next 

    Application.EnableEvents = True 
End If 
End Sub 

回答

0

首先,你已經SetPivotField這裏:

Set pPF = pPT.PivotFields(sField) 

所以您可以在以下行中使用它。

pPF.LabelRange.Group _ 
Start:=True, End:=True, Periods:= _ 
    Array(False, False, False, False, True, True, True) 

但是,則還需要添加Cells參考,喜歡的東西:

pPF.LabelRange.Group.Cells(2, 1) _ 
Start:=True, End:=True, Periods:= _ 
    Array(False, False, False, False, True, True, True) 

注意:你可能還需要檢查By參數。

+0

感謝曬。是的,我注意到我已經設置了我的透視圖字段,然後沒有使用它!這是一個「解決」我的代碼試圖讓它工作的人工產物。你會看到我通過選擇數據範圍來解決這個問題,我猜想它與使用引用類似。 –

0

我設法得到它的工作使用此代碼:

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim sField As String, sFieldName As String 
Dim oRF As Object 
Dim pPT As PivotTable 

If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub 
If Target.Address = "$F$3" Then 
    Application.EnableEvents = False 
    sField = Target.Value 
    sFieldName = Target.Offset(0, 1).Value 

    For Each pPT In ActiveSheet.PivotTables 
     'Remove all pivot fields for all pivot tables in this sheet 
     For Each oRF In pPT.RowFields 
      Debug.Print oRF.Name 
      oRF.Orientation = xlHidden 
     Next 

     'Add the new pivot field 
     pPT.PivotFields(sField).Orientation = xlRowField 
     pPT.PivotFields(sField).Position = 1 
     pPT.PivotCache.Refresh 
     pPT.CompactLayoutRowHeader = sFieldName 

     'If the pivot field is a date field, group by month and year 
     If InStr(UCase(sFieldName), "DATE") > 0 Then 


      pPT.PivotFields(sField).DataRange.Select 
      Selection.Group _ 
       Start:=True, End:=True, Periods:= _ 
       Array(False, False, False, False, True, False, True) 
     End If 
    Next 
    ActiveSheet.AutoFilter.ApplyFilter 
    Range(Target.Address).Select 
    Application.EnableEvents = True 
End If 
End Sub