Inconsistent Output for Empty Columns in a CSV File

Written by Allen Wyatt (last updated January 1, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


Mark noticed an odd thing when it comes to creating CSV files with Excel: the files are not always consistent in how they end each row in the output data. When he creates a CSV file that has, perhaps, 70 field (columns) of data and then views the CSV file in a text editor, he noticed that the records all contain carriage returns, but in different places. Some records have a string of commas representing empty fields, then terminate in the 'right' place; others end right after the last populated column; some have a few commas, but not enough for all the empty fields. Mark wondered why this occurs, and how he can get the CSV files to contain a consistent number of output fields.

One relatively easy way around the issue is to include a fully populated "dummy" field in your data, before you save as a CSV. For instance, if your table has 70 columns in it, at cell A71 enter a period. Copy the contents of this cell downward, for as many rows as you have in the table. When you then export the worksheet to CSV, Excel will include the dummy field, but more importantly will include the proper number of field delimiters (commas) before that final field in each record.

If you don't want the dummy field, you can try this:

  1. Select one of the cells in your header row.
  2. Press Shift+Ctrl+8. The entire data table is selected.
  3. Press Ctrl+H to display the Replace tab of the Find and Replace dialog box. (See Figure 1.)
  4. Figure 1. The Replace tab of the Find and Replace dialog box.

  5. Make sure the Find What box is completely empty.
  6. In the Replace With box enter a single space.
  7. Click Replace All. Excel should inform you how many replacements were done.

These steps replace all the empty cells with cells that contain a single space. You can then do the export to CSV and the proper number of fields will be exported for every single row.

Finally, if you routinely export large tables to CSV format, you may wish to create a macro that does the file creation for you. The following is just one example of the type of macro you can use:

Sub CreateCSV()
    Dim wkb As Workbook
    Dim wks As Worksheet
    Dim wksOri As Worksheet
    Dim iCols As Integer
    Dim lRow As Long
    Dim iCol As Integer
    Dim lRows As Long
    Dim sFilename As String

    Application.ScreenUpdating = False
    sFilename = "C:\test.csv"
    Set wksOri = ActiveSheet
    iCols = wksOri.Cells. _
        SpecialCells(xlCellTypeLastCell).Column
    lRows = wksOri.Cells. _
        SpecialCells(xlCellTypeLastCell).Row

    Set wkb = Workbooks.Add
    Set wks = wkb.Worksheets(1)

    For lRow = 1 To lRows
        For iCol = 1 To iCols
            With wks.Cells(lRow, 1)
                If iCol = 1 Then
                    .Value = wksOri.Cells(lRow, iCol).Text
                Else
                    .Value = .Value & "," & _
                        wksOri.Cells(lRow, iCol).Text
                End If
            End With
        Next
    Next

    Application.DisplayAlerts = False
    wkb.SaveAs FileName:=sFilename, _
        FileFormat:=xlCSV
    wkb.Close
    Application.DisplayAlerts = True
    wksOri.Parent.Activate
    Application.ScreenUpdating = True
    MsgBox sFilename & " saved"

    Set wks = Nothing
    Set wkb = Nothing
    Set wksOri = Nothing
End Sub

The macro creates a brand-new workbook and then "compiles" into column A of the workbook's first worksheet the information from the original worksheet. This data, which will contain a delimiter for every single field in the original, is then saved as a CSV file. Finally, the temporary workbook is deleted.

The path and filename of the CSV is hard-coded into the code (the sFileName variable), though it could be modified to have the code ask for a filename if desired.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3068) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Bold Turning On by Itself

Word always relies on styles to define how text appears in your document. If you don't understand how Word applies ...

Discover More

Missing Header and Footer Toolbar

When you need to make changes to the header or footer of a document, the Header and Footer toolbar is invaluable. What if ...

Discover More

Counting Asterisks in a Column

Excel can be used as a simple database program. If you use asterisks in a column of your database to designate ranking of ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Saving All Open Workbooks

Wouldn't it be nice to have a single command that would save each of you open workbooks, all at once? It's easy to do ...

Discover More

Working with Lotus 1-2-3 Spreadsheets

If you've got some older data around your office that started in an old Lotus 1-2-3 system, you may want to open it in ...

Discover More

Comma-Delimited Differences for PC and Mac

When you choose to save worksheet data in CSV format, Excel gives you three choices for file formats. Those choices are ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.