在VBA中创建具有多个值的行的数据透视表

时间:2015-10-29 07:36:14

标签: excel vba excel-vba

我想创建一个如下所示的数据透视表:

Pivot Effect

我查看了Microsoft,它只教我如何为[SKU船舶数量总和]创建一列:

ActiveWorkbook.ActiveSheet.Select  
Range("A1").Select  
Set objTable = Sheet1.PivotTableWizard

Set objField = objTable.PivotFields("Deal ID & SKU")  
objField.Orientation = xlRowField  

Set objField = objTable.PivotFields("SKU Ship quantity")  
objField.Orientation = xlDataField

但是这只为SKU Ship数量创建了一列,当我尝试使用相同的脚本添加[GROSS SELL TO(LC)]时,数据透视表就会失真。 我有几列要添加。我怎么能实现这个目标?

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

ActiveWorkbook.ActiveSheet.Select
Range("A1").Select
Set objTable = Sheet1.PivotTableWizard(TableDestination:=Sheets.Add.Range("A3"))

With objTable
    .PivotFields("Deal ID & SKU").Orientation = xlRowField
    .PivotFields("SKU Ship quantity").Orientation = xlDataField
    .PivotFields("GROSS SELL TO (LC)").Orientation = xlDataField
    With .DataPivotField
        .Orientation = xlColumnField
        .Position = 1
    End With
End With