Vba 仅用于 ListObject 总计计算的数字格式

Vba Number Format for only Totals Calculation for ListObject

我只想在总行table创建自定义货币数字格式,如果有解决方案,我在屏幕截图中标记为黄色。

谢谢 杰克

Sub test2()
Dim objListObj As ListObject
Set objListObj = Sheets("test").ListObjects(1)
objListObj.ShowTotals = True
With Sheets("test").ListObjects("Table1")
.ListColumns("Total").TotalsCalculation = xlTotalsCalculationSum
.ListColumns("Pot. :").TotalsCalculation = xlTotalsCalculationSum
.ListColumns("Total End :").TotalsCalculation = xlTotalsCalculationSum
.ListColumns("PRICE2").TotalsCalculation = xlTotalsCalculationSum
End With
End Sub

我录制了一个宏,结果是这样的:

Selection.NumberFormat = "#,###,##0.0000000000000 [$?-x-xbt1]"

我是怎么找到这种格式的?好吧,我在一个单元格中单击右键,要求“设置单元格格式”,我选择了“数字”、“货币”,我选择了与我想要的最相似的那个,然后单击“自定义”以查看实际格式规则,之后我可以修改,如本例所示:

(您可以在“类型”字段中看到的格式规则会自动填写。)

Excel Table (ListObject): TotalsCalculation & TotalsRowRange

Option Explicit

Sub test2()
    
    Dim Headers As Variant
    Headers = Array("Total", "Pot. :", "Total End :", "PRICE2")
    
    With ThisWorkbook.Worksheets("test").ListObjects("Table1")
        .ShowTotals = True
        Dim n As Long
        For n = LBound(Headers) To UBound(Headers)
            With .ListColumns(Headers(n))
                .TotalsCalculation = xlTotalsCalculationSum
                .Parent.TotalsRowRange.Columns(.Index) _
                    .NumberFormat = "#,##0.00 $"
            End With
        Next n
    End With

End Sub