简体   繁体   English

使用VBA制作数据透视表时,“值”字段不显示值

[英]Value Field not Displaying Values when using VBA to make a pivot table

The code does not cause an error and a table with the correct row and column fields are created. 该代码不会导致错误,并且将创建具有正确的行和列字段的表。 Nothing appears in the value field though. 但是,值字段中什么也没有出现。

'Insert Row Fields
With ActiveSheet.PivotTables("Rev by Facility")
    With .PivotFields("Facility")
        .Orientation = xlRowField
        .Position = 1
    End With
    'Insert Column Fields
    With .PivotFields("Revenue")
        .Orientation = xlColumnField
        .Position = 1
    End With
    'Insert Data Field
    With .PivotFields("SubTotal 2")
        .Orientation = xlValuesField
        .Position = 1
        .Function = xlSum
        .Name = "Sum of SubTotal 2"
    End With
End With

This is only part of the code, the above code should not cause the value field to not appear. 这只是代码的一部分,上面的代码不应导致value字段不出现。 The data source of the pivot table includes the column and all the data I want to appear in the value field. 数据透视表的数据源包括该列以及我要显示在value字段中的所有数据。

Not having your lines properly indented can cause all sorts of issues. 线条缩进不正确会导致各种问题。 Try this 尝试这个

    'Insert Row Fields
    With ActiveSheet.PivotTables("Rev by Facility")
        With .PivotFields("Facility")
            .Orientation = xlRowField
            .Position = 1
        End With
    'Insert Column Fields
        With .PivotFields("Revenue")
            .Orientation = xlColumnField
            .Position = 1
        End With
    'Insert Data Field
        With .PivotFields("SubTotal 2")
            .Orientation = xlValuesField
            .Position = 1
            .Function = xlSum
            .Name = "Sum of SubTotal 2"
        End With
    End With

To define a pivot field as "value field", you have to use xlDataField . 要将枢轴字段定义为“值字段”,必须使用xlDataField

.Orientation = xlDataField

Just in case: 以防万一:
The often called "filter fields" have the orientation xlPageField . 通常称为“过滤器字段”的方向为xlPageField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM