Generating CSV file for Excel, how to have a newline inside a value

calendar_today Asked Aug 6, 2009
thumb_up 41 upvotes
history Updated April 14, 2026

Direct Answer

You should have space characters at the start of fields ONLY where the space characters are part of the data. Excel will not strip off leading spaces. You will get unwanted spaces…. This is a prose walkthrough, ranked #4th of 303 by community upvote score, from 2009.


The Problem (Q-score 88, ranked #4th of 303 in the Excel VBA archive)

The scenario as originally posted in 2009

I need to generate a file for Excel, some of the values in this file contain multiple lines.

there’s also non-English text in there, so the file has to be Unicode.

The file I’m generating now looks like this: (in UTF8, with non English text mixed in and with a lot of lines)

Header1,Header2,Header3
Value1,Value2,"Value3 Line1
Value3 Line2"

Note the multi-line value is enclosed in double quotes, with a normal everyday newline in it.

According to what I found on the web this supposed to work, but it doesn’t, at least not win Excel 2007 and UTF8 files, Excel treats the 3rd line as the second row of data not as the second line of the first data row.

This has to run on my customer’s machines and I have no control over their version of Excel, so I need a solution that will work with Excel 2000 and later.

Thanks

EDIT: I “solved” my problem by having two CSV options, one for Excel (Unicode, tab separated, no newlines in fields) and one for the rest of the world (UTF8, standard CSV).

Not what I was looking for but at least it works (so far)

Why community consensus is tight on this one

Across 303 Excel VBA entries in the archive, the accepted answer here holds elite answer (top 10 %%) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — elite answer (top 10 %%) (+41)

Verbal answer — walkthrough without a code block

Note: the verified answer is a prose walkthrough. If you need a runnable sample, check Excel VBA entries ranked in the top 10 of the same archive.

You should have space characters at the start of fields ONLY where the space characters are part of the data. Excel will not strip off leading spaces. You will get unwanted spaces in your headings and data fields. Worse, the " that should be “protecting” that line-break in the third column will be ignored because it is not at the start of the field.

If you have non-ASCII characters (encoded in UTF-8) in the file, you should have a UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file. Otherwise Excel will interpret the data according to your locale’s default encoding (e.g. cp1252) instead of utf-8, and your non-ASCII characters will be trashed.

Following comments apply to Excel 2003, 2007 and 2013; not tested on Excel 2000

If you open the file by double-clicking on its name in Windows Explorer, everything works OK.

If you open it from within Excel, the results vary:

  1. You have only ASCII characters in the file (and no BOM): works.
  2. You have non-ASCII characters (encoded in UTF-8) in the file, with a UTF-8 BOM at the start: it recognises that your data is encoded in UTF-8 but it ignores the csv extension and drops you into the Text Import not-a-Wizard, unfortunately with the result that you get the line-break problem.

Options include:

  1. Train the users not to open the files from within Excel 🙁
  2. Consider writing an XLS file directly … there are packages/libraries available for doing that in Python/Perl/PHP/.NET/etc


When to Use It — vintage (14+ years old, pre-2013)

A top-10 Excel VBA pattern — why it still holds up

Ranks #4th of 303 in the Excel VBA archive. The only pattern ranked immediately above it is “How should I escape commas and speech marks in CSV files so…” — compare both if you’re choosing between approaches.

What changed between 2009 and 2026

The answer is 17 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

Why is this answer the top decile of Excel VBA Q&A?
expand_more

Answer score +41 vs the Excel VBA archive median ~13; this entry is elite. The score plus 88 supporting upvotes on the question itself (+88) means the asker and 40 subsequent voters all validated the approach.

The answer has no code block — how do I turn it into a snippet?
expand_more

Use the walkthrough above as a checklist, then open a top-10 Excel VBA archive entry for a concrete starting template you can adapt.

This answer is 17 years old. Is it still relevant in 2026?
expand_more

Published 2009, which is 17 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #3?
expand_more

The pattern one rank above is “How should I escape commas and speech marks in CSV files so they work in Excel?”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 88, Answer-score 41, original post 2009, ranked #4th of 303 in the Excel VBA archive. Last regenerated April 14, 2026.