.NumberFormat sometimes returns the wrong value with dates and times

前端 未结 3 1056
深忆病人
深忆病人 2020-12-11 23:07

It seems that every week or so someone posts a question about dates being converted (corrupted?) to American format. Like many others, I have attempted to help but the prob

3条回答
  •  长情又很酷
    2020-12-11 23:59

    I have mainly come up against formatting and date issues when opening text files which have been saved with different regional settings. Two useful cell properties for dealing with this are:

    • .Text returns the cell value as it is displayed
    • .Value2 returns the unformatted cell value or date serial number.

    As you say, standard date and number formats depend on windows regional settings and this may not be desired behavior as the same workbook can display differently in different regions. MS introduced the regional code prefixes in number formats (circa Excel 2000?) which enforce consistent display if needed but they need to be explicitly selected.

    If you really want to see a date or number as the user entered it, you could extract the contents of the .xlsx file looking at the worksheet cell format and the shared strings xml definitions which list the number formats in the saved workbook. I don't really see a need to do this though as the underlying value is stored internally as a serial number and this will not change.

提交回复
热议问题