I have an excel file after reading it and storing in a data table the format is changing here i want to convert that string into a decimal format and write into another excel file
Example:
Input file After storing it into a data table Actual result i want
Amount Amount Amount
20000.00 20000 20000.00
12345.00 12345 12345.00
As per my understanding you are getting amount as string and want it to be a decimal with two places. You can use the following syntax to your work done.
string.Format(“{0:0.000}”, Cint(amount)).
It didn’t work in my scenario where I am building a data table with datatype decimal for column Amount after formatting it is displaying in the same format where precision of two digits are not coming
If you give something like 5000.00 it is writing only 5000 in excel but when you give an input like 5000.456 something it is writing 5000.45 correctly. I have attached the screenshot for both scenarios below kindly have look into it.
I am also attaching the code with excel writing part included along with excel sheet.
Yes I got the solution.
In my case i need the custom format #0.00 so that i have done following steps.
1.Make the Excel as visible.
2.send Ctrl+1 hot key ,it will open the format cell window.
3.click on custom method.
4.use type into and type #0.00
5.click on ok button.
6.send ctrl+s hot key for saving.
you can use below mentioned macro to format the numbers instead going through the front end.
Sub NumberFormat()
On Error Resume Next
Range(“A1”).Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.NumberFormat = “#,###”
Range(“A1”).Select
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.NumberFormat = “#,###”
Range(“A1”).Select
End Sub