Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于条件的VBA透视表列_Vba_Excel_Pivot Table - Fatal编程技术网

基于条件的VBA透视表列

基于条件的VBA透视表列,vba,excel,pivot-table,Vba,Excel,Pivot Table,我正在寻找有关透视表列的帮助,让我解释一下 我有一个包含数据和日期列的Excel数组 我需要按特定顺序显示数据,将上周的数据总和显示在一列中,将本周的数据显示在下一列中 ColA ColB ColC ColD 58330 4120166225 65122908794 20/03/2015 00003 5575067588 23588912840 12/03/2015 50112 0

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

我有一个包含数据和日期列的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等提供的示例只是一个示例,不在代码中。

这很容易混淆,因为您没有更改代码以指示哪些实际列名与ColA to ColD的示例数据对齐。寒冷的日子是显而易见的。我不知道为什么重命名了示例数据列,但在代码中保留了真实的列名。我建议您首先使用插入数据透视表和数据透视表工具栏手动构建数据透视表。一旦您获得了手动创建的数据透视表,然后将这些设置编码到VBA中。要获取“this week/last week”列,您需要计算一个新的数据列,将日期映射为周。感谢您的输入,我的代码现在已经完全正常工作了,我现在唯一关心的是获取我的两个数据透视表列(上周和本周的数据数),我不知道如何处理