Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2188
  • Last Modified:

Excel: Add double quotes on exported CSV from field

I tried to export CSV with this command:
ActiveWorkbook.SaveAs Filename:=fileresult, FileFormat:=xlCSV, CreateBackup:=False

But the exported CSV doesn't have double quote for each cell except the "Wrap Text" ones (item.csv as attached)

Then i tried to add this command:
 Set SrcRg = ActiveSheet.UsedRange
    For Each CurrRow In SrcRg.Rows
        For Each CurrCell In CurrRow.Cells
            CurrCell.Value = Chr(34) & CurrCell.Value & Chr(34)

it visually display the double quotes, but when i open the raw source of CSV, it consist 3 double quotes, for example:

(Item2.csv as attached)

So... how to solve this problem ?

1 Solution
Shanmuga SundaramDirector of Software EngineeringCommented:
I dont know how to fore see your problem. the quickest option, since you know vba is to write to a text try as below

Sub main()

Open "C:\results.csv" For Output As #1
Set SrcRg = ActiveSheet.UsedRange
    For Each CurrRow In SrcRg.Rows
        For Each CurrCell In CurrRow.Cells
            Print #1, """" & CurrCell.Value & """,";
        Print #1, ""
Close #1

End Sub
Please see the following website if it can give you a hint.
Shanmuga SundaramDirector of Software EngineeringCommented:
please see the excel with macro and results attached
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

The quotes are being added to the "wrap text" fields because they contain one or more comma characters.

Since the Chr(34) character is, itself,  used as a delimiter in CSV files, they must be handled differently.  The three consecutive quote character sequences you see are the Chr(34) character that is being delimited by quote characters.

I'm not sure I fully understand what you need.  Have you considered tab-separated (delimited) values, rather than comma-separated?
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
veematicsAuthor Commented:
Hello i was out of town for almost a month...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now