VBA code to copy sheet and save it as csv loses precision, even in integers!

Denise Draper

New Member
Joined
Jul 28, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to save each sheet of an xlsm file into its own CSV. In order not to mess up the current workbook, I decided to do this by copying the cells of each sheet to a new workbook then saving that workbook (which has only one sheet) as csv. The relevant subroutine is this:

VBA Code:
Sub Save_Sheet_As_CSV(sheetname As String, filename As String)
    ' Make a CSV file for sheet in the same directory as the current workbook
    ' To save a single sheet without affecting the current workbook, we make a new
    ' workbook, copy the sheet to it, save that workbook, then close it.
    
    Application.ScreenUpdating = False

    Dim dir As String
    Dim sht As Worksheet
    Dim tmpbook As Workbook
    
    dir = ActiveWorkbook.Path
    Set sht = Sheets(sheetname)
    Set tmpbook = Workbooks.Add
    outname = dir & Application.PathSeparator & filename
    
    ' Activate the original sheet to make sure any activation macros have run (Detailed Results does some)
    sht.Activate
    ' Copy over the Cells (not the Sheet) so that we don't get any of that pesky macro stuff.
    sht.Cells.Copy
    tmpbook.Sheets(1).Cells.PasteSpecial Paste:=xlPasteValues

    ' Save the temporary workbook, suppressing "Do you want to overwrite" dialog
    Application.DisplayAlerts = False
    tmpbook.SaveAs filename:=outname, FileFormat:=xlCSVUTF8
    Application.DisplayAlerts = True

    ' Bye bye
    tmpbook.Close
    Application.ScreenUpdating = True
End Sub

This seemed to work fine, but on closer inspection, data are being truncated. Look at the two examples below: the first is from the original spreadsheet, the second from the CSV I created.
This seems absolutely crazy to me; I can't believe there would be a bug of this magnitude so clearly I am doing something wrong, but I don't have the faintest idea what.
BTW, if I do just "save as CSV" from Excel, the result is correct. And changing the output format in the code above to xlWorkbookDefault also produces the correct results.
What could be happening here?

Original table:
46,661.08-$163,536,860,805.70-$163,536,860,805.70
47,015.70-$164,779,719,348.63-$328,316,580,154.33
47,373.24-$166,032,845,670.56-$494,349,425,824.90
47,734.17-$167,297,822,434.09-$661,647,248,258.99
35,601.03-$124,773,809,310.84-$786,421,057,569.82
24,279.23-$85,093,391,185.97-$871,514,448,755.80
13,709.49-$48,048,753,414.48-$919,563,202,170.28
3,836.00-$13,444,324,915.73-$933,007,527,086.01
5,393.06$18,901,495,033.52-$914,106,032,052.49


When saved as CSV and re-opened:
46,661.08163,537,000,000.00163,537,000,000.00
47,015.70164,780,000,000.00328,317,000,000.00
47,373.24166,033,000,000.00494,349,000,000.00
47,734.17167,298,000,000.00661,647,000,000.00
35,601.03124,774,000,000.00786,421,000,000.00
24,279.2385,093,391,186.00871,514,000,000.00
13,709.4948,048,753,414.00919,563,000,000.00
3,836.0013,444,324,916.00933,008,000,000.00
5,393.0618,901,495,034.00914,106,000,000.00
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Change the line
VBA Code:
    tmpbook.Sheets(1).Cells.PasteSpecial Paste:=xlPasteValues
to
VBA Code:
    tmpbook.Sheets(1).Cells.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
...and it should work. Pasting the values pastes a number larger than can be handled whereas the xlPasteFormulasAndNumberFormats ensures your formatting is retained (i.e. saved to the .csv as text/quoted cells).
 
Upvote 0
Change the line
VBA Code:
    tmpbook.Sheets(1).Cells.PasteSpecial Paste:=xlPasteValues
to
VBA Code:
    tmpbook.Sheets(1).Cells.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
...and it should work. Pasting the values pastes a number larger than can be handled whereas the xlPasteFormulasAndNumberFormats ensures your formatting is retained (i.e. saved to the .csv as text/quoted cells).
Actually it may be xlPasteValuesAndNumberFormats you need
 
Upvote 0
Solution
That did it! Thank you so much. I didn't think the number formats would affect the saved value, but I can see that it wrote them out in exponential format, and that was the problem.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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