
Tips.Net > ExcelTips Home > Files > Inconsistent Output for Empty Columns in a CSV File
Summary: When you create a CSV file in Excel, the information stored in the file may not contain all the fields that you think it should. This tip examines the reasons behind this behavior, along with several different ways to work around the problem. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Mark Warner 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.
This is actually a problem that has been known to Microsoft for quite some time. The Microsoft Knowledge Base includes an article on this topic as early as Excel 97:
http://support.microsoft.com/?kbid=77295
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:
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 temproary 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.
Tip #3068 applies to Microsoft Excel versions: 97 2000 2002 2003
Got the Time? Understanding the ins and outs of working with times and dates can be confusing. Remove the confusion--ExcelTips: Times and Dates is an invaluable resource for learning how best to work with times and dates.
Check out ExcelTips: Times and Dates today!
Add power to your purpose with Excel. A comprehensive 500+ page e-book explains everything you need to know about macros. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site