Excel VBA - 格式化数据透视表(.NumberFormat)不起作用

时间:2016-10-11 13:24:46

标签: excel vba

目的

创建一个拉入规范化数据集的宏。将数据集放入数据透视表中以便于解释。

APPROACH

  1. 规范化数据
  2. 选择规范化数据并创建数据透视表
  3. 配置数据透视表标题和列
  4. 格式标题和列
  5. CODE

    Sub createPivot()
    
    Dim ws As Worksheet
    Dim pvtCache As pivotCache
    Dim pvt As pivotTable
    Dim srcData As String
    Dim lastRow As Long
    Dim startPvt As String
    Dim target As Worksheet
    
    'Delete previous pivottable
    Worksheets("PIVOT").PivotTables("PivotTable1").TableRange2.Clear
    
    
    'Select pivot table data
    Worksheets("CONSOLIDATED").Activate
    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    srcData = ActiveSheet.Name & "!" & Range("A1:H" & lastRow).Address(ReferenceStyle:=xlR1C1)
    
    'Set pivot table location
    Set target = ThisWorkbook.Worksheets("PIVOT")
    startPvt = target.Name & "!" & target.Range("A1").Address(ReferenceStyle:=xlR1C1)
    
    'Create pivot cache
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=srcData)
    
    'Deploy pivot table
    Set pvt = pvtCache.CreatePivotTable( _
        TableDestination:=startPvt, _
        TableName:="PivotTable1")
    
        'Add Pivot Fields
        pvt.PivotFields("Fiscal Year").Orientation = xlColumnField
        pvt.PivotFields("Fiscal Year").Position = 1
    
        pvt.PivotFields("Fiscal Month").Orientation = xlColumnField
        pvt.PivotFields("Fiscal Month").Position = 2
    
        pvt.PivotFields("Unit").Orientation = xlRowField
        pvt.PivotFields("Unit").Position = 1
    
        pvt.PivotFields("Project").Orientation = xlRowField
        pvt.PivotFields("Project").Position = 2
    
        pvt.PivotFields("Base Expense").Orientation = xlDataField
    
        'Format cells
        pvt.PivotFields("Base Expense").NumberFormat = "$ #,##0"
    
    
    End Sub
    

    问题

    • 没有错误,但pvt.PivotFields("Base Expense").NumberFormat = "$ #,##0"似乎没有生效。

    问题

    1. 我不确定为什么VBA不会抛出错误,但在给定正确的参数时拒绝格式化表格。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:0)

我刚才写了这个宏来格式化我的数据透视表。修改代码以满足您的需求应该非常容易。它循环遍历每个值字段,并将函数更改为sum并格式化值。

Sub formatPivot()
    Dim pvtTbl As PivotTable
    Dim pvtName As String
    Dim pvtType As String
    Dim pvtFld As PivotField

    pvtName = ActiveCell.PivotTable.Name                ' Get the name of the active pivottable
    Set pvtTbl = ActiveSheet.PivotTables(pvtName)       ' set the pivot table to the active table

    ' Change to sum and update the number format
    For Each pvtFld In pvtTbl.DataFields
        pvtType = pvtFld                                    
        With pvtTbl.PivotFields(pvtType)
            .Function = xlSum
            .NumberFormat = "#,##0"
        End With
    Next
End Sub

答案 1 :(得分:-1)

今天我才发现,如果有帮助,您不能对标签字段进行数字格式化,而只能对数据字段进行格式化。

https://docs.microsoft.com/en-us/office/vba/api/excel.pivotfield.numberformat

”备注 您只能为数据字段设置NumberFormat属性。

格式代码与“设置单元格格式”对话框中的“格式代码”选项相同。格式函数使用与NumberFormat和NumberFormatLocal属性不同的格式代码字符串。“

相关问题