简体   繁体   English

如何将 excel 格式设置为 0.00% 到 0.0000 十进制值

[英]How do I set excel format 0.00% to 0.0000 decimal value

we need to export some data into excel which is fine, data is in percentages and hence I provide我们需要将一些数据导出到 excel 中,这很好,数据以百分比表示,因此我提供

cell.NumberFormat = "0.00%"

The data type of exported values is decimal and hence the above format works fine.导出值的数据类型是十进制,因此上述格式工作正常。

but the problem happens when value is 0.00000000000000000 excel fails to set a format to that and hence when we open the exported excel file it says但是当值是0.00000000000000000时会出现问题,excel 无法设置格式,因此当我们打开导出的 excel 文件时,它说

Excel completed file level validation and repair. Excel 完成了文件级验证和修复。 Some parts of this workbook may have been repaired or discarded.此工作簿的某些部分可能已被修复或丢弃。 Repaired Records: Cell information from /xl/worksheets/sheet.xml part修复的记录:来自 /xl/worksheets/sheet.xml 部分的单元格信息

and all the cells that have value as 0 show a tag on them saying -并且所有值为 0 的单元格上都显示一个标签,上面写着 -

The number in this cell is formatted as text or is preceded by an apostrophe.此单元格中的数字格式为文本或前面有撇号。

When value is something like 0.023443434343 excel formats it as 2.34% and format is Percentage with 2 decimal places;当值类似于0.023443434343 excel 将其格式化为2.34% ,格式为百分比, 0.023443434343 2 个小数位; I am not sure why it is not able to provide format to 0 value.我不确定为什么它不能提供 0 值的格式。

Read it into a double, then format and write it back.将其读入双精度,然后格式化并写回。 I don't get any errors that way.那样我不会有任何错误。

Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim d As Double

d = ws.Range("C14").Value
ws.Range("C14").NumberFormat = "0.00%"
ws.Range("C14").Value = d

I'm not clear if your data is already in the worksheet or not.我不清楚您的数据是否已经在工作表中。 If it is not then put the data into the double from the source.如果不是,则将数据从源放入 double 中。

d = Val("0.00000000000000000")
ws.Range("C14").NumberFormat = "0.00%"
ws.Range("C14").Value = d

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

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