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.
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.
NumberFormat = "00000" ' Display 12345678 as 12,345,678. worksheet. Cells("C1"). Value = 12345678 worksheet.
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With