Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting - add comma, 2 decimal places to cell values vb.net excel interop

Tags:

c#

excel

vb.net

vba

Does anyone have any vb.net or vba code that will format excel values or a range of cells to have comma for 100s, 1000s,10000s etc.. and 2 decimal places only.

i.e. 10,256.45

like image 854
Nick LaMarca Avatar asked Jun 22 '10 20:06

Nick LaMarca


2 Answers

With a reference to the range:

rng.NumberFormat = "#,##0.00"

The current selection is also a range, so if you want it to work with the selection, just use:

Selection.NumberFormat = "#,##0.00"
like image 108
Jay Avatar answered Nov 14 '22 21:11

Jay


If you don't need a specific custom formatting you can use Excel styles:

allRange.Style = "Comma"; // 1234.5678 -> 1,234.56
// or
allRange.Style = "Comma [0]"; // 1234.5678 -> 1,234
like image 43
HuBeZa Avatar answered Nov 14 '22 22:11

HuBeZa