Format numbers while printing csv

usercogn

New Member
Joined
Jun 19, 2011
Messages
8
hi,
I'm trying to create a csv file.
Is there any way to keep the same figures format as in the initial excel file when printing the csv or shall we define the format of every column in the loop(and how)?

Actually in the excel file, some columns contain figures that must be formatted as 0.00000(5decimals mandatory even if zero). But when printing the csv with this loop, the useless ending 0 are deleted...

Below is the current code :
Code:
Sub Export_As_Semicolon_delimited()      Dim objSaveBox As FileDialog     Dim MYFILE As String     Dim r As Long, c As Long          Const DELIMITER = ";"          Sheets("INF").Select      '    ChDrive = "C:\"     ' set default save drive '    ChDir = "C:\Temp"   ' set default save directory (can't change default drive with just this)     Set objSaveBox = Application.FileDialog(msoFileDialogSaveAs)     With objSaveBox         .InitialFileName = Range("A3") & "_INF_" & Range("C3") & ".csv"         '.FilterIndex = 15         If .Show = -1 Then             MYFILE = .SelectedItems.Item(1)         Else             Exit Sub         End If     End With          'Write cells from A2 to (last row, last column) to a Semicolon delimted text file     FileNum = FreeFile     Open MYFILE For Output As #FileNum     For r = 2 To Range("A2").End(xlDown).Row         For c = 1 To Range("A2").End(xlToRight).Column             Print #FileNum, Cells(r, c).Value & DELIMITER;         Next c         Print #FileNum,     Next r     Close #FileNum          MsgBox "File saved as a Semicolon delimited text file." & _             vbLf & vbLf & MYFILE, , "Save Complete"  End Sub</pre>

thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
There are no formatting allowances in a CSV file. It's a flat text file when you're done, so no formatting needs of any kind are preserved. Ever. To have "formatting", you will need to save to an Excel file format.
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top