标准上的VBA透视表列

时间:2015-03-23 21:30:56

标签: excel vba excel-vba pivot-table

我正在寻找数据透视表列的帮助,让我解释一下

我有一个包含数据和日期列的Excel数组 我需要以特定的顺序显示数据,列中上周的数据总和以及下一列中当前周的数据

ColA      ColB            ColC               ColD
58330     4120166225     65122908794     20/03/2015
00003     5575067588     23588912840     12/03/2015
50112     0652930006     66859544716     21/03/2015
66000     6058624450     89001420698     22/03/2015
19150     0035396046     47033160700     15/03/2015
69220     5478494542     78292321587     23/03/2015

以下是代码:

 Sub createPivotTable1()
'refer Image DataSource for source data. Note that the complete data table extends upto row number 49.
'refer Image 1 for PivotTable report created after running below code

Dim PvtTbl As PivotTable
Dim wsData As Worksheet
Dim rngData As Range
Dim PvtTblCache As PivotCache
Dim wsPvtTbl As Worksheet
Dim pvtFld As PivotField

'determine the worksheet which contains the source data
Set wsData = Worksheets("Feuil3")

'determine the worksheet where the new PivotTable will be created
Set wsPvtTbl = Worksheets("Report")

'delete all existing Pivot Tables in the worksheet
'in the TableRange1 property, page fields are excluded; to select the entire PivotTable report, including the page fields, use the TableRange2 property.
For Each PvtTbl In wsPvtTbl.PivotTables
If MsgBox("Supprimer le tableau croisé dynamique existant?!", vbYesNo) = vbYes Then
PvtTbl.TableRange2.Clear
End If
Next PvtTbl


'A Pivot Cache represents the memory cache for a PivotTable report. Each Pivot Table report has one cache only. Create a new PivotTable cache, and then create a new PivotTable report based on the cache.

'set source data range:
Set rngData = wsData.Range("A1:AG49")


'for creating a Pivot Cache (version excel 2007), use the PivotCaches.Create Method. When version is not specified, default version of the PivotTable will be xlPivotTableVersion12.

'The PivotCache.CreatePivotTable method creates a PivotTable report based on a Pivot Cache. TableDestination is mandatory to specify in the method.

'create Pivot Cache and PivotTable version excel 2007:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:=wsPvtTbl.Range("A1"), TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12

Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1")

'Default value of ManualUpdate property is False wherein a PivotTable report is recalculated automatically on each change. Turn off automatic updation of Pivot Table during the process of its creation to speed up code.
PvtTbl.ManualUpdate = True


'add row, column and page (report filter) fields:
Set pvtFld = PvtTbl.PivotFields("ACENERG201")
pvtFld.Orientation = xlPageField

Set pvtFld = PvtTbl.PivotFields("ACOBS001")
pvtFld.Orientation = xlRowField

Set pvtFld = PvtTbl.PivotFields("TELEFON")
pvtFld.Orientation = xlRowField
pvtFld.Position = 2

Set pvtFld = PvtTbl.PivotFields("ACOBS001")
pvtFld.Orientation = xlRowField


'set data field - specifically change orientation to a data field and set its function property:
With PvtTbl.PivotFields("TELEFON")
.Orientation = xlDataField
'.Function = xlSum
'.NumberFormat = "#,##0"
.Position = 1
End With


'turn on automatic update / calculation in the Pivot Table
PvtTbl.ManualUpdate = False

'Application.Version Property - returns the Excel version number in which VBA is being executed viz. 9.0, 10.0, 11.0, 12.0, etc.
MsgBox Application.Version

'PivotTable.Version Property - returns the PivotTable version number, 0, 1, 2, 3, etc.
MsgBox PvtTbl.Version


End Sub

我知道我错过了一些代码,但我不知道如何解决这个问题。请耐心等待,ColA,ColB等提供的示例就是一个示例,不在代码中。

0 个答案:

没有答案