Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number Formatting in excel Vb.Net

Tags:

c#

excel

vb.net

Can someone give me some code to set the cell of an excel spreadsheet to a number format that use a max of 2 decimal places. Or would it work better it I change the data I am putting into the cell to a proper format? Here is a sample of data that is going in. Right now its going in as a string... col("ADJ").ToString() where col is a DataTable oject.

like image 845
Nick LaMarca Avatar asked Apr 08 '10 19:04

Nick LaMarca


People also ask

How do I change the number format in Excel VBA?

Now go to VBA editor and create a macro name. After selecting the cell to select the property called “NumberFormat” by putting dot (.) After selecting the property put an equal sign. Now apply the format we wish to apply in this case, format is date format i.e. “dd-mmm-yyyy” format.

How do I display 12345678 on cell C1?

NumberFormat = "00000" ' Display 12345678 as 12,345,678. worksheet. Cells("C1"). Value = 12345678 worksheet.


2 Answers

I'm not sure about VB, but in C# it would be:

worksheet.Cells["C3"].NumberFormat = "0.00"

or

worksheet.Cells["A:Z"].NumberFormat = "0.00"

or

Range.NumberFormat = "0.00"
like image 162
Igby Largeman Avatar answered Sep 30 '22 15:09

Igby Largeman


Here's one way:

Selection.NumberFormat = """R"" #,##0.00;""R"" -#,##0.00"

Another way:

worksheet.Cells(x,y).NumberFormat = """R"" #,##0.00;""R"" -#,##0.00"

NOTE: this may vary with different versions of Excel.

like image 42
RBarryYoung Avatar answered Sep 30 '22 14:09

RBarryYoung