Excel VBA在数据透视表中添加值字段不起作用

时间:2019-03-27 18:49:38

标签: excel vba

嗨,我想在数据透视表中添加一个值字段。这是我现在拥有的代码:

 'Insert Row Fields
   With ActiveSheet.PivotTables("PivotTable").PivotFields("Account Lvl1 Desc")
   .Orientation = xlRowField
   .Position = 1
   End With
   With ActiveSheet.PivotTables("PivotTable").PivotFields("Account Description")
   .Orientation = xlRowField
   .Position = 2
   End With
   With ActiveSheet.PivotTables("PivotTable").PivotFields("Supplier")
   .Orientation = xlRowField
   .Position = 3
   End With

 'Insert Data Field
   With ActiveSheet.PivotTables("PivotTable")

   .PivotFields ("Distrib Amt CAD")
   .Orientation = xlDataField
   .Function = xlSum
   '.NumberFormat = "#,##0"
   .Name = "Sum of OH in CAD "
   End With

其他所有方法都起作用,但价值观

1 个答案:

答案 0 :(得分:0)

我建议进行一些清理:

With ActiveSheet.PivotTables("PivotTable")
   'Insert Row Fields
   With .PivotFields("Account Lvl1 Desc")
       .Orientation = xlRowField
       .Position = 1
   End With
   With .PivotFields("Account Description")
       .Orientation = xlRowField
       .Position = 2
   End With
   With .PivotFields("Supplier")
       .Orientation = xlRowField
       .Position = 3
   End With
   'Insert Data Field 
   With .PivotFields ("Distrib Amt CAD")
       .Orientation = xlDataField
       .Function = xlSum
       '.NumberFormat = "#,##0"
       .Name = "Sum of OH in CAD "
   End With
End With

如果您具有从SharePoint字段表导出的值,则这些值是字符串...在数据上运行与以下代码类似的代码,以便您可以将其显示为数字:

With sws 'source worksheet
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    For i = 2 To lr
        .Cells(i, 6).Value = Val(.Cells(i, 6).Value) * 1 'Uses column 6 (F)
    Next i
End With

一旦存在适当的数据,它将在输出中提供超过#Value错误。