繁体   English   中英

数据透视表数据范围的 VBA 数字格式不起作用

[英]VBA Number format of Pivot-Table's data range doesn't work

Sub Cost()

    Dim CurrentPivot As String

    On Error GoTo OnError

    Expand

    CurrentPivot = ActiveSheet.Name

    ActiveSheet.PivotTables(CurrentPivot).PivotFields("Sum of Hours"). _
        Orientation = xlHidden
    ActiveSheet.PivotTables(CurrentPivot).AddDataField ActiveSheet.PivotTables( _
        CurrentPivot).PivotFields("Cost"), "Sum of Cost", xlSum
    FormatProj
    Zero
    Range("B43:AJ5000").Select
    Selection.NumberFormat = "$#,##0.00;[Red]$#,##0.00"

OnError:
    MsgBox ("Viewing data in $Cost")

    ActiveWorkbook.ShowPivotTableFieldList = False
    Range("B44").Select

End Sub

我的号码看起来仍然像2615233.698

下面的代码将PivotTable的范围格式化为所需的格式。

代替使用Range("B43:AJ5000").Select和后来的Selection ,您可以直接访问数据透视表的范围并使用以下命令修改其格式:

PvtTbl.TableRange2.NumberFormat = "$#,##0.00;[Red]$#,##0.00"

Option Explicit

Sub Cost()

Dim CurrentPivot As String
Dim PvtTbl      As PivotTable

On Error GoTo OnError
'Expand ' <-- not sure what does this Sub-routine actually does

CurrentPivot = ActiveSheet.Name

' set the Pivot-Table object
Set PvtTbl = ActiveSheet.PivotTables(CurrentPivot)
With PvtTbl
    .PivotFields("Sum of Hours").Orientation = xlHidden
    .AddDataField .PivotFields("Cost"), "Sum of Cost", xlSum

    'FormatProj ' <-- not sure what does this Sub-routine actually does
    'Zero ' <-- not sure what does this Sub-routine actually does

    .TableRange2.NumberFormat = "$#,##0.00;[Red]$#,##0.00"
End With

OnError:
MsgBox "Viewing data in $Cost"

ActiveWorkbook.ShowPivotTableFieldList = False
Range("B44").Select

End Sub

'定义名称数据透视表集 tbl = wkstbl.PivotTables("ResumoEstados")

With tbl
    teste = wks.Name & "!" & rngDinamica.CurrentRegion.Address
    .ChangePivotCache ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    teste, Version:=6)
    .RefreshTable

'使用数据透视表选择的数据主体范围
With.DataBodyRange.Cells.NumberFormat = "#,##0.00" 结束方式 结束方式

暂无
暂无

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

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