VBA格式功能,自定义格式可将一个DP设置为数百万个数字

时间:2018-08-10 14:56:06

标签: excel vba formatting

我正在Excel 2010中运行一些VBA,以将电子表格中的表格中的数字输出到外部PowerPoint演示文稿中。由于涉及的数字多种多样,我使用宏正在读取的表中指定的自定义格式字符串来确定输出格式,如下所示:

CellData = Format(SourceData,FormattingCode)

对于百万个数字(0 ,,)和百分比(0.0%),我能够毫不费力地使用它。但是,我现在想做几百万个数字,但显示一位小数,即1,200,000应该呈现为1.2

通常执行此操作的格式代码是(0.0 ,,),我可以看到它在常规单元格格式设置中有效。但是,当我将其传递给format函数时,我将数字舍入到小数点后一位而不取整,没有1200000.0这样的分隔符。

有人知道为什么会这样吗?我知道Format函数与自定义格式略有不同,因为显而易见的原因,它不支持任何单元格对齐功能,但是看不出为什么这应该有所不同。有谁知道我如何获得该功能以输出所需的格式?

非常感谢,

2 个答案:

答案 0 :(得分:0)

VBA格式功能无法处理此格式字符串。所以:

Sub dural()
    Dim SourceData As Long
    Dim CellData As String
    SourceData = 1200000
    CellData = Format(SourceData, "0.0,,")
    MsgBox CellData
End Sub

将输入不正确的字符串。改用它:

Sub dural2()
    Dim SourceData As Long, D As Double
    Dim CellData As String

    SourceData = 1200000
    D = SourceData / 1000000
    CellData = Format(D, "0.0")

    MsgBox CellData
End Sub

enter image description here

EDIT#1:

使用一个单元格。数字处理。格式化。转换为字符串:

Sub dural3()
    Dim SourceData As Long, D As Double
    Dim CellData As String, r As Range
    Set r = Range("A1")
    SourceData = 1200000
    D = SourceData / 1000000

    With r
        .Clear
        .Value = D
        .NumberFormat = "0.0"
        .Value = .Text         'convert to text
        CellData = .Value
    End With
    MsgBox CellData
End Sub

enter image description here

答案 1 :(得分:0)

> if (typeof require !== 'undefined') XLSX = require('xlsx'); > > var workbook = XLSX.readFile('tempPublic\\MainSub.xlsx', { sheetStubs: true }); > var sheet_name_list = workbook.SheetNames; > var ws = workbook.Sheets[sheet_name_list[0]] > res.send(XLSX.utils.sheet_to_json(ws)) > > > [ > { > "#": 1, > "Main category": "Mobile", > "Sub1": "Phone" > }, > { > "Sub1": "Tablet" > }, > { > "Sub1": "Smart Watch" > }, > { > "Sub1": "Fitness Technology" > }, > { > "Sub1": "Accessires", > "Sub2": "Headsets & Speakers" > }, > { > "Sub2": "Cables & Adapters" > }, > { > "Sub2": "Chargers & Power Bank" > }, > { > "Sub2": "Batteries" > }, > { > "Sub2": "Covers & Screan Protection" > }, > { > "Sub2": "Car Kit" > }, > { > "Sub2": "Memory Card" > }, > { > "Sub2": "VR Devices" > }, > { > "Sub2": "Bluetooth Devices" > }, > { > "Sub2": "Watch Strap" > }, > { > "#": 2, > "Main category": "Clothing & Fashion ", > "Sub1": "Men", > "Sub2": "Shirts" > }, > { > "Sub2": "Arabic costume (Dashdasha)" > }, > { > "Sub2": "T-Shirts & Polos" > }, > { > "Sub2": "Jeans" > }, > { ...... .....

这在SSRS RDL的#,0,,.0函数中起作用。我实际的全部是 format()