This project has moved and is read-only. For the latest updates, please go here.

Format cell value as percent

Oct 14, 2011 at 10:05 AM

I have a problem with formatting text as %. Example:

Worksheet.Cells["B6"].Value = "15";
Worksheet.Cells["B6"].Style.Numberformat.Format = "0%";

When I open document done by code above the text in B6 is aligned to the left, there is a message like "The number in this cell is formatted as text or preceded by an apostrophe". A chart which uses this value draws incorrectly.  When I double-click the cell (when open in Excel) the formatting is ok. What's interesting when the value is set by a formula, then the formatting is ok. Any clue what am I doing wrong?

Oct 19, 2011 at 6:41 AM

You set the value as text. Set the value as a number instead...

Worksheet.Cells["B6"].Value = 15;
Worksheet.Cells["B6"].Style.Numberformat.Format = "0%";
Oct 19, 2011 at 11:05 AM

Man, I had the answer right in front of me. Thanks!