Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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,我正在尝试使用vba从名为“数据”的工作表中的数据创建透视表。以多个栏目组织,其中三个栏目被称为NOM、NOM_MOVE和COMMENT Receptive 我希望数据透视表具有以下字段:列标签字段=总和(值),行标签字段=注释,值字段=NOM之和,NOM_移动之和 但是,我编写的代码不起作用,也没有提供正确的字段(即使是使用数据透视表向导和缓存/创建数据透视表的不同形式) 在分配正确字段时遇到问题之前,我的代码是: Sub CreatePivot() ' Create the 5 summar

我正在尝试使用vba从名为“数据”的工作表中的数据创建透视表。以多个栏目组织,其中三个栏目被称为NOM、NOM_MOVE和COMMENT Receptive

我希望数据透视表具有以下字段:列标签字段=总和(值),行标签字段=注释,值字段=NOM之和,NOM_移动之和

但是,我编写的代码不起作用,也没有提供正确的字段(即使是使用数据透视表向导和缓存/创建数据透视表的不同形式)

在分配正确字段时遇到问题之前,我的代码是:

Sub CreatePivot()
' Create the 5 summary pivot tables using the TRADE_LIST sheet as data
Dim pivot1 As Worksheet
Dim datasheet As Worksheet
Dim dataCache As PivotCache
Dim PVT As PivotTable
Dim LastRow As Long
Dim LastCol As Long
Dim dataSource As Range
Dim PVTdest As Range

Set datasheet = Sheets("DATA")
Set pivot1 = Sheets("PIVOT")

' (1) Source data from the TRADE_LIST sheet and create a pivot cache from source data                     to use for each pivot table

LastRow = datasheet.Cells(1, 1).End(xlDown).Row
LastCol = datasheet.Cells(1, 1).End(xlToRight).Column
Set dataSource = datasheet.Cells(1, 1).Resize(LastRow, LastCol)

Set dataCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=dataSource)

' (2) Create a pivot table
Set PVTdest = pivot1.Cells(2, 2)
Set PVT = dataCache.CreatePivotTable(tabledestination:=PVTdest,              tablename:="Comment_Sums")


非常感谢任何帮助

我对您的代码做了一些修改,请尝试以下方法:

Sub CreatePivot()
' Create the 5 summary pivot tables using the TRADE_LIST sheet as data
Dim LastRow, LastCol As Long
Dim dataSource, destination As String
Dim wb As Workbook
Dim data_sheet, pivot1 As Worksheet

Set wb = ThisWorkbook
Set data_sheet = wb.Sheets("DATA") 'in your code, you forgot to include WB
Set pivot1 = wb.Sheets("PIVOT")

' (1) Source data from the TRADE_LIST sheet and create a pivot cache from source data _
  to use for each pivot table

'i changed this part to cover all data 
LastRow = data_sheet.Cells(data_sheet.Rows.Count, 1).End(xlUp).Row
LastCol = data_sheet.Cells(1, data_sheet.Columns.Count).End(xlToLeft).Column

'PivotCache creation requires arguments in string format, no need to use Set
dataSource = data_sheet.Name & "!" & data_sheet.Cells(1, 1).Resize(LastRow, LastCol).Address(ReferenceStyle:=xlR1C1)
destination = pivot1.Name & "!" & pivot1.Range("A1").Address(ReferenceStyle:=xlR1C1)

'here we use the datasource and destination
wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    dataSource, Version:=xlPivotTableVersion12).CreatePivotTable _
    TableDestination:=destination, TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion12

'after creating pivot, add the Row Field
With pivot1.PivotTables("PivotTable1").PivotFields("Comment")
    .Orientation = xlRowField
    .Position = 1
End With

'then add the column fields
With pivot1
    .PivotTables("PivotTable1").AddDataField .PivotTables( _
        "PivotTable1").PivotFields("NOM"), "Sum of NOM", xlSum
    .PivotTables("PivotTable1").AddDataField .PivotTables( _
        "PivotTable1").PivotFields("NOM_MOVE"), "Sum of NOM_MOVE", xlSum
End With

End Sub
希望这是你的开始