简体   繁体   中英

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

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

Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded. Repaired Records: Cell information from /xl/worksheets/sheet.xml part

and all the cells that have value as 0 show a tag on them saying -

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; I am not sure why it is not able to provide format to 0 value.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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