在 Excel Pivot Table 中设置组选择的动态日期
Set dynamic dates for Group Selection in Excel Pivot Table
我有一个从 TFS 中提取的数据集,它获取了过去一年 (@Today-365) 的数据。数据由 Excel 抓取,然后用于填充数据透视表,这些数据透视表按 28 天的日期周期分组。我想使小组日期自动化,但无法使我的代码正常工作或无法在“网络”上找到好的示例。
这是我到目前为止得到的...
Sub SetStartDate()
' Sets the date for the Start Date, End Date, and Groupings for the Date Periods
'
Dim wbk As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Dim pvtFld As PivotField
Dim startDate As Date
Dim endDate As Date
Dim rowLabel As String
'Application.ScreenUpdating = False ' Turn on Later
Set wbk = ActiveWorkbook
Set ws = wbk.ActiveSheet
Set startDate = Today() 'incomplete
Set endDate = Today()-365days 'incomplete
For Each pvt In ws.PivotTables
Debug.Print "Pivot Name = " & pvt.name
Debug.Print "Pivot Field Count = " & pvt.PivotFields.Count
For Each pvtFld In pvt.RowFields
Debug.Print "Pivot Field = " & pvtFld.name ' Name of Pivot field
rowLabel = pvtFld.name
Debug.Print "Row Label = " & rowLabel
'THIS LINE DOESN'T WORK, BUT THIS IS WHERE I RUN INTO TROUBLE IN MY CODE
pvtFld.Selection.Group start:=CLng(startDate), End:=CLng(endDate), By:=28, Periods:=Array(False, False, False, True, False, False, False)
Next pvtFld
Next pvt
'Application.ScreenUpdating = True ' Remove comment later
End Sub
正如我在中间状态的评论,我不确定在弄清楚 rowField 名称是什么之后该去哪里。
我在发布几分钟后偶然发现了自己的解决方案 - 希望这对其他人有所帮助....
Sub SetStartDate()
' Sets the date for the Start Date, End Date, and Groupings for the Date Periods
Dim wbk As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Dim pvtFld As PivotField
Dim startDate, endDate As Long
Dim pivotName, rowLabel As String
Application.ScreenUpdating = False
Set wbk = ActiveWorkbook
Set ws = wbk.ActiveSheet
startDate = ws.Range("P2").Value ' Location of StartDate in Spreadsheet
endDate = ws.Range("P1").Value ' Location of EndDate in Spreadsheet
For Each pvt In ws.PivotTables
For Each pvtFld In pvt.RowFields
Debug.Print "Pivot Field = " & pvtFld.name
rowLabel = pvtFld.name
' Debug.Print "Row Label = " & rowLabel
pvtFld.DataRange.Group start:=startDate, End:=endDate, By:=28, Periods:=Array(False, False, False, True, False, False, False)
Next pvtFld
Next pvt
Application.ScreenUpdating = True
End Sub
我有一个从 TFS 中提取的数据集,它获取了过去一年 (@Today-365) 的数据。数据由 Excel 抓取,然后用于填充数据透视表,这些数据透视表按 28 天的日期周期分组。我想使小组日期自动化,但无法使我的代码正常工作或无法在“网络”上找到好的示例。
这是我到目前为止得到的...
Sub SetStartDate()
' Sets the date for the Start Date, End Date, and Groupings for the Date Periods
'
Dim wbk As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Dim pvtFld As PivotField
Dim startDate As Date
Dim endDate As Date
Dim rowLabel As String
'Application.ScreenUpdating = False ' Turn on Later
Set wbk = ActiveWorkbook
Set ws = wbk.ActiveSheet
Set startDate = Today() 'incomplete
Set endDate = Today()-365days 'incomplete
For Each pvt In ws.PivotTables
Debug.Print "Pivot Name = " & pvt.name
Debug.Print "Pivot Field Count = " & pvt.PivotFields.Count
For Each pvtFld In pvt.RowFields
Debug.Print "Pivot Field = " & pvtFld.name ' Name of Pivot field
rowLabel = pvtFld.name
Debug.Print "Row Label = " & rowLabel
'THIS LINE DOESN'T WORK, BUT THIS IS WHERE I RUN INTO TROUBLE IN MY CODE
pvtFld.Selection.Group start:=CLng(startDate), End:=CLng(endDate), By:=28, Periods:=Array(False, False, False, True, False, False, False)
Next pvtFld
Next pvt
'Application.ScreenUpdating = True ' Remove comment later
End Sub
正如我在中间状态的评论,我不确定在弄清楚 rowField 名称是什么之后该去哪里。
我在发布几分钟后偶然发现了自己的解决方案 - 希望这对其他人有所帮助....
Sub SetStartDate()
' Sets the date for the Start Date, End Date, and Groupings for the Date Periods
Dim wbk As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Dim pvtFld As PivotField
Dim startDate, endDate As Long
Dim pivotName, rowLabel As String
Application.ScreenUpdating = False
Set wbk = ActiveWorkbook
Set ws = wbk.ActiveSheet
startDate = ws.Range("P2").Value ' Location of StartDate in Spreadsheet
endDate = ws.Range("P1").Value ' Location of EndDate in Spreadsheet
For Each pvt In ws.PivotTables
For Each pvtFld In pvt.RowFields
Debug.Print "Pivot Field = " & pvtFld.name
rowLabel = pvtFld.name
' Debug.Print "Row Label = " & rowLabel
pvtFld.DataRange.Group start:=startDate, End:=endDate, By:=28, Periods:=Array(False, False, False, True, False, False, False)
Next pvtFld
Next pvt
Application.ScreenUpdating = True
End Sub