monty.py
monty.py

Reputation: 2789

Excel Range apply new NumberFormat

I have an Microsoft.Office.Interop.Excel.Range which I fill with Data and set a new NumberFormat, like that:

wkbActive = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
wksActive = (Excel.Worksheet)wkbActive.ActiveSheet;
Excel.Range rngExample = wksActive.get_Range("A1");
rngExample.Value = "08.06.2015";
rngExample.NumberFormat = "ddd, d. M";
example.NumberFormat = "ddd, d. M";

The problem is that the new NumberFormat isn´t apply until I click into the cell. Is there an "update" Method or sth for the Range?!

Upvotes: 0

Views: 550

Answers (1)

Charles Mager
Charles Mager

Reputation: 26223

This is because you're setting the cell value to a string and this is not evaluated as a date until you click into the cell and press 'Enter'.

Excel stores dates as double which represents a time since 1900. You should set the Value2 property to a DateTime per this example:

rngExample.Value2 = new DateTime(2015, 6, 8);
rngExample.NumberFormat = "ddd, d. M";

Upvotes: 1

Related Questions