Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Excel 2007 - saving as a .csv no longer encases the fields in inverted commas

Posted on 2011-03-07
Medium Priority
Last Modified: 2012-06-27
I appreciate that this question has already been posed - see "Export to CSV options in Excel 2007", but I am looking for a better solution.

In earlier versions of Excel (before 2007) when you Saved As a .csv file the fields would be encased in inverted commas, e.g. "123","ABC","XYZ"    But now I get  123,ABC,XYZ

I need the inverted commas because the .csv file is imported into a Visual Basic program.  I don't want to make the import procedure any more complicated for my users, so adding Access to the mix isn't really an option.  Without reverting to Excel 2003, does anyone know of a workaround for this problem?

Thank you
Question by:lybreeze
  • 2
  • 2
LVL 10

Expert Comment

ID: 35062438
Something like this code will work.  Just change the range accordingly.  Note that this one will have "" around EVERY cell in the range

Sub runit()
    Write_CSV_File "myfile.csv", Sheets("Sheet1").Range("A1:Z5000")
End Sub

Sub Write_CSV_File(ByVal filename As String, output_data)
Dim Lastrow, lastcolumn, rowcount, columncount As Long
Lastrow = UBound(output_data)
lastcolumn = UBound(Application.Transpose(output_data))
    Open filename For Output As #1      'open CSV file
        For rowcount = 1 To Lastrow
            For columncount = 1 To lastcolumn
                output_data(rowcount, columncount) = """" & output_data(rowcount, columncount) & """"
            Next columncount
            Print #1, Join(Application.WorksheetFunction.Index(output_data, rowcount, 0), ",")
        Next rowcount
    Close #1    'close CSV file

End Sub

Open in new window

LVL 10

Accepted Solution

Makrini earned 1500 total points
ID: 35062466
By the way - usually Excel will assume inverted commas if there are spaces in the cell.  This is not ideal for CSV and I NEVER trust Excel to write my csv files.  This is the reason for the code.

  I use a modified version of this with a text ini file for each csv I write.  The text file contains the formats for all the columns I wish to format.  In this way I can also write out dates in the format I actually want to...

Author Comment

ID: 35082194
Hi Makrini,
Many thanks for your reply.  I guess that I would need to run this as a macro.  A problem will be that the file has a different name every time.  However, I'll give it a go and report back.

What a nuisance that we have to do this when it worked perfectly in Excel 2000/03

Thanks again

Author Closing Comment

ID: 35137322
Thank you for your replies.  However, I think that I got confused with the way that Excel works in respect of .csv files, and there is not a difference between Excel 2003 and 2007 in this regard.

I have made the following observations :-
*   a .csv file created or saved by any version of Excel will only have double-quotes around those fields that contain a comma, e.g. an Excel row of    ABC    123    WX,YZ    will become   ABC,123,"WX,YZ"  in a .csv file.  This happens regardless of whether the original file was a .xls or a .csv file
*   if a field already contains a double-quote then Excel will duplicate those double-quotes in a .csv file, so   "Chez Nous"    Albert Square      in Excel, becomes    ""Chez Nous"",Albert Square    in .csv
*   if an Excel row contains empty cells at the end, Excel will normally add commas to indicate that there is no data in those cells (it also does this with empty cells in the middle of a row)
*   the 3 methods outlined above mean that a .csv file can usually be imported into a VB program without a problem
*   however, sometimes Excel does not insert the commas at the end to indicate empty cells. This often happens when the spreadsheet is large and contains rows of varying lengths.  This can cause VB imports to fail because the number of fields imported is not the number that was expected.
*   another problem is that Excel will convert telephone numbers into a numeric format if there isn't a space in the middle of the number, and it will therefore strip off the leading zero
*  to conclude, it is much better if the app. that creates the .csv file does so with every field encased in double-quotes, including any empty fields at the end of the record.  The only problem then is if there are any double-quotes within any of the fields, but these should be programmed out at source.  VB programs can import such a .csv file provided there are no double-quotes within any of the fields

Makrini's code could certainly help in cases where you don't have control over the app. that creates the .csv file, as it would then encase each field in double-quotes.  I guess you would run it as a macro?


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question