Excel trailing comma bug in csv files - excel

I have an XLS file creates a CSV file with a macro on Excel 2003, I have 40+ columns and the last 3 ones are optional, therefore there are a lot of empty values on the XLS, when I run the exporting subroutine it wont put a trailing comma on all of the rows, why ? Because : http://support.microsoft.com/kb/77295 -.-
In Microsoft Office Excel, if you save a file in the text or in the CSV (comma separated value) format, Excel puts tabs or commas between each column of the worksheet. However, certain text files may be saved with a different number of tabs or commas in 16-row blocks.
here is their suggested work around:
To make sure that Excel saves tab or comma delimiters for all empty columns, verify that the last column in the file contains some data in at least every 16 rows throughout the file. If the blocks of rows do not contain data, add spaces or other characters in every 16 rows to the cells in the last column, or reorder the columns in the worksheet so that the last column on the worksheet always contains information.
-.- way to go microsoft ! -.-
Ok so my main issue is that the generated file will be parsed by another program out of my scope which needs that specific format as it is right now I'm adding a blank every 16 row if the field is empty, this seems to do it but Data Processing Deparment is complaining about that white space... can you believe them!?
Anyway I also tried to add a flag and remove it with the find function, but ofc when you save the file it will take away the delimiters again...
thanks for reading my history ;p
Any suggestions ?
Edit: Unfortunately using Excel is must, the data is manually inputted through the excel sheet by different users, the vba codes does a lot more like template generation etc.. this is the only issue that I'm having and it is not feasible to change the whole process.

Manual example;
Dim r As Range: Set r = Range("A1:C10") '// or ActiveSheet.UsedRange for everything
Dim buffer As String, delimiter As String, c As Range
Dim i As Long
For Each c In r
If (i Mod r.Columns.Count) = 0 Then
If (Len(buffer)) Then delimiter = vbCrLf
Else
delimiter = ","
End If
buffer = buffer & delimiter & """" & CStr(c.Value) & """" '//or c.text to preserve formatting
i = (i + 1)
Next
Open "c:\xxx.csv" For Output As #1
Print #1, buffer
Close #1

If you don't have commas in your data, it would be very easy to write a text reader that goes through and counts the number of columns in a line and just add commas to the end if there aren't enough. It's not ideal, but it's probably easier than writing a custom Excel to CSV converter.

Simplest way:
ActiveWorkbook.SaveAs "MyFileNameAndDir.csv", xlCSV, Local:=True

Suggestion 1) Stop using Excel to generate a CSV file.
What's your data source? Where does Excel get the data from? Perhaps you can do something with the original data source to generate a CSV instead of using Excel as the middleman. If you like really big hammers, Biztalk is the ultimate MS tool for data input/output manipulation. Few can pull that one out of their toolbox, but throwing together a custom c# program to generate a CSV file can be done in a couple hours.
In short: If at all possible, use a better tool!

Related

How to Append CSV file via VBA without reading empty formatted cells

I have a code that is able to Append a range of data in a worksheet to an existing CSV file. However, I noticed that when the existing CSV file contains empty, but formatted cells beyond the end of the data range, the Append function takes into account those empty cells as well.
For example, the existing CSV file has 10 rows of data, while rows 11 to 20 are empty, but has been formatted (e.g. as "dd-mmm-yy"). So when I append the CSV file, the new data is added at row 21 instead of row 11.
Apart from manually deleting rows 11 to 20 in the CSV file, is there a quick fix to this? I have many existing CSV files, so it is not feasible to do the manual way. As to why some of the rows are empty, but formatted, it is due to some earlier amendments that took place.
Appreciate the help.
I have a code that is able to Append a range of data in a worksheet to an existing CSV file. However, I noticed that when the existing CSV file contains empty, but formatted cells beyond the end of the data range, the Append function takes into account those empty cells as well.
If you've written that code, it needs to account for the formatted cells you want to ignore. Let's say you're exchanging data from a .xlsx file to a .csv file and all of this is being done inside the Excel platform. Then for example, if "General" is the format that works best for you, but some trailing cells in .csv column A, Sheet 1 are not that, then have your code evaluate on a loop whether CSV Worksheets("Book1").Range("A[whatever row]").NumberFormat = "General"
Then have your code change the ones that aren't, to General; then proceed with the append.

Retain comma in returned value from t() function in Excel

We allow users to export data in a CSV format from an Oracle database, which opens up in Excel in a new window.
To retain the data as it is in the database, each column uses the =t() function to keep text as text.
However....some of the data fields have commas in them. So where we have a column of:
=t("12345,67890")
you end up with 2 columns in Excel rather than one column with the whole string in.
Any suggestion on how to retain the use of the t() function AND stop the data splitting into two columns?
Easiest solution is to change your output to prefix all text values with a ' (single apostrophe) instead of T() and change the delimiter that the Oracle DB outputs to TAB. The ' does the same as the T() functions (forces Excel to parse the cell value as a string) but seems somewhat more elegant. You have to change the delimiter as it cannot be contained inside the data as Excel won't be able to differentiate them. After changing the output then it just becomes a trivial matter of opening the file as a Delimited text file and selecting TAB as the delimiter. Hope this helps.

CSV file contains trailing pipe delimiters

I have an excel file and I exported the file into a pipe delimited csv file. However, in each row of the csv file, there are trailing pipes.
Here's a typical row:
dsad|asd|safd|sadaf| |||||||||
ddss|sd|saadfdaf|dadf |||||||||
Does anyone know how not to include those trailing pipes while exporting to csv from excel?
Thanks.
The sheet you are exporting probably contains cell that excel believes has values in them (I call them ghost cells). To prevent this behaviour, I have two suggestions:
Copy the spreadsheet to a fresh new spreasheet and export again. It should be fine.
Delete all the columns after the last column containing data (Do not use the Delete/Del key, select the columns till the end and use Ctrl+- instead). After this, save the workbook then export again.
If those don't work, there's probably some rows with actual data somewhere, and there, the data will be correctly delimited by the pipes. Might be best to count the number of columns of data that you have and make sure your resulting csv file has the same number of columns.
If this still doesn't work... just shout I guess? ^^;

Stop Excel from automatically converting certain text values to dates

Does anyone happen to know if there is a token I can add to my csv for a certain field so Excel doesn't try to convert it to a date?
I'm trying to write a .csv file from my application and one of the values happens to look enough like a date that Excel is automatically converting it from text to a date. I've tried putting all of my text fields (including the one that looks like a date) within double quotes, but that has no effect.
I have found that putting an '=' before the double quotes will accomplish what you want. It forces the data to be text.
eg. ="2008-10-03",="more text"
EDIT (according to other posts): because of the Excel 2007 bug noted by Jeffiekins one should use the solution proposed by Andrew: "=""2008-10-03"""
I know this is an old question, but the problem is not going away soon. CSV files are easy to generate from most programming languages, rather small, human-readable in a crunch with a plain text editor, and ubiquitous.
The problem is not only with dates in text fields, but anything numeric also gets converted from text to numbers. A couple of examples where this is problematic:
ZIP/postal codes
telephone numbers
government ID numbers
which sometimes can start with one or more zeroes (0), which get thrown away when converted to numeric. Or the value contains characters that can be confused with mathematical operators (as in dates: /, -).
Two cases that I can think of that the "prepending =" solution, as mentioned previously, might not be ideal is
where the file might be imported into a program other than MS Excel (MS Word's Mail Merge function comes to mind),
where human-readability might be important.
My hack to work around this
If one pre/appends a non-numeric and/or non-date character in the value, the value will be recognized as text and not converted. A non-printing character would be good as it will not alter the displayed value. However, the plain old space character (\s, ASCII 32) doesn't work for this as it gets chopped off by Excel and then the value still gets converted. But there are various other printing and non-printing space characters that will work well. The easiest however is to append (add after) the simple tab character (\t, ASCII 9).
Benefits of this approach:
Available from keyboard or with an easy-to-remember ASCII code (9),
It doesn't bother the importation,
Normally does not bother Mail Merge results (depending on the template layout - but normally it just adds a wide space at the end of a line). (If this is however a problem, look at other characters e.g. the zero-width space (ZWSP, Unicode U+200B)
is not a big hindrance when viewing the CSV in Notepad (etc),
and could be removed by find/replace in Excel (or Notepad etc).
You don't need to import the CSV, but can simply double-click to open the CSV in Excel.
If there's a reason you don't want to use the tab, look in an Unicode table for something else suitable.
Another option
might be to generate XML files, for which a certain format also is accepted for import by newer MS Excel versions, and which allows a lot more options similar to .XLS format, but I don't have experience with this.
So there are various options. Depending on your requirements/application, one might be better than another.
Addition
It needs to be said that newer versions (2013+) of MS Excel don't open the CSV in spreadsheet format any more - one more speedbump in one's workflow making Excel less useful... At least, instructions exist for getting around it. See e.g. this Stackoverflow: How to correctly display .csv files within Excel 2013?
.
Working off of Jarod's solution and the issue brought up by Jeffiekins, you could modify
"May 16, 2011"
to
"=""May 16, 2011"""
I had a similar problem and this is the workaround that helped me without having to edit the csv file contents:
If you have the flexibility to name the file something other than ".csv", you can name it with a ".txt" extension, such as "Myfile.txt" or "Myfile.csv.txt". Then when you open it in Excel (not by drag and drop, but using File->Open or the Most Recently Used files list), Excel will provide you with a "Text Import Wizard".
In the first page of the wizard, choose "Delimited" for the file type.
In the second page of the wizard choose "," as the delimiter and also choose the text qualifier if you have surrounded your values by quotes
In the third page, select every column individually and assign each the type "Text" instead of "General" to stop Excel from messing with your data.
Hope this helps you or someone with a similar problem!
2018
The only proper solution that worked for me (and also without modifying the CSV).
Excel 2010:
Create new workbook
Data > From Text > Select your CSV file
In the popup, choose "Delimited" radio button, then click "Next >"
Delimiters checkboxes: tick only "Comma" and uncheck the other options, then click "Next >"
In the "Data preview", scroll to the far right, then hold shift and click on the last column (this will select all columns). Now in the "Column data format" select the radio button "Text", then click "Finish".
Excel office365: (client version)
Create new workbook
Data > From Text/CSV > Select your CSV file
Data type detection > do not detect
Note: Excel office365 (web version), as I'm writing this, you will not be able to do that.
WARNING: Excel '07 (at least) has a(nother) bug: if there's a comma in the contents of a field, it doesn't parse the ="field, contents" correctly, but rather puts everything after the comma into the following field, regardless of the quotation marks.
The only workaround I've found that works is to eliminate the = when the field contents include a comma.
This may mean that there are some fields that are impossible to represent exactly "right" in Excel, but by now I trust no-one is too surprised.
While creating the string to be written to my CSV file in C# I had to format it this way:
"=\"" + myVariable + "\""
In Excel 2010 open a new sheet.
On the Data ribbon click "Get External Data From Text".
Select your CSV file then click "Open".
Click "Next".
Uncheck "Tab", place a check mark next to "Comma", then click "Next".
Click anywhere on the first column.
While holding the shift key drag the slider across until you can click in the last column, then release the shift key.
Click the "text" radio button then click "Finish"
All columns will be imported as text, just as they were in the CSV file.
Still an issue in Microsoft Office 2016 release, rather disturbing for those of us working with gene names such as MARC1, MARCH1, SEPT1 etc.
The solution I've found to be the most practical after generating a ".csv" file in R, that will then be opened/shared with Excel users:
Open the CSV file as text (notepad)
Copy it (ctrl+a, ctrl+c).
Paste it in a new excel sheet -it will all paste in one column as long text strings.
Choose/select this column.
Go to Data- "Text to columns...", on the window opened choose "delimited" (next). Check that "comma" is marked (marking it will already show the separation of the data to columns below) (next), in this window you can choose the column you want and mark it as text (instead of general) (Finish).
HTH
Here is the simple method we use at work here when generating the csv file in the first place, it does change the values a bit so it is not suitable in all applications:
Prepend a space to all values in the csv
This space will get stripped off by excel from numbers such as " 1"," 2.3" and " -2.9e4" but will remain on dates like " 01/10/1993" and booleans like " TRUE", stopping them being converted into excel's internal data types.
It also stops double quotes being zapped on read in, so a foolproof way of making text in a csv remain unchanged by excel EVEN IF is some text like "3.1415" is to surround it with double quotes AND prepend the whole string with a space, i.e. (using single quotes to show what you would type) ' "3.1415"'. Then in excel you always have the original string, except it is surrounded by double quotes and prepended by a space so you need to account for those in any formulas etc.
(Assuming Excel 2003...)
When using the Text-to-Columns Wizard has, in Step 3 you can dictate the data type for each of the columns. Click on the column in the preview and change the misbehaving column from "General" to "Text."
This is a only way I know how to accomplish this without messing inside the file itself. As usual with Excel, I learned this by beating my head on the desk for hours.
Change the .csv file extension to .txt; this will stop Excel from auto-converting the file when it's opened. Here's how I do it: open Excel to a blank worksheet, close the blank sheet, then File => Open and choose your file with the .txt extension. This forces Excel to open the "Text Import Wizard" where it'll ask you questions about how you want it to interpret the file. First you choose your delimiter (comma, tab, etc...), then (here's the important part) you choose a set columns of columns and select the formatting. If you want exactly what's in the file then choose "Text" and Excel will display just what's between the delimiters.
(EXCEL 2007 and later)
How to force excel not to "detect" date formats without editing the source file
Either:
rename the file as .txt
If you can't do that, instead of opening the CSV file directly in excel, create a new workbook then go to
Data > Get external data > From Text and select your CSV.
Either way, you will be presented with import options, simply select each column containing dates and tell excel to format as "text" not "general".
What I have done for this same problem was to add the following before each csv value:
"="""
and one double quote after each CSV value, before opening the file in Excel. Take the following values for example:
012345,00198475
These should be altered before opening in Excel to:
"="""012345","="""00198475"
After you do this, every cell value appears as a formula in Excel and so won't be formatted as a number, date, etc. For example, a value of 012345 appears as:
="012345"
None of the solutions offered here is a good solution. It may work for individual cases, but only if you're in control of the final display. Take my example: my work produces list of products they sell to retail. This is in CSV format and contain part-codes, some of them start with zero's, set by manufacturers (not under our control). Take away the leading zeroes and you may actually match another product.
Retail customers want the list in CSV format because of back-end processing programs, that are also out of our control and different per customer, so we cannot change the format of the CSV files. No prefixed'=', nor added tabs. The data in the raw CSV files is correct; it's when customers open those files in Excel the problems start. And many customers are not really computer savvy. They can just about open and save an email attachment.
We are thinking of providing the data in two slightly different formats: one as Excel Friendly (using the options suggested above by adding a TAB, the other one as the 'master'. But this may be wishful thinking as some customers will not understand why we need to do this. Meanwhile we continue to keep explaining why they sometimes see 'wrong' data in their spreadsheets.
Until Microsoft makes a proper change I see no proper resolution to this, as long as one has no control over how end-users use the files.
I have jus this week come across this convention, which seems to be an excellent approach, but I cannot find it referenced anywhere. Is anyone familiar with it? Can you cite a source for it? I have not looked for hours and hours but am hoping someone will recognize this approach.
Example 1: =("012345678905") displays as 012345678905
Example 2: =("1954-12-12") displays as 1954-12-12, not 12/12/1954.
Hi I have the same issue,
I write this vbscipt to create another CSV file. The new CSV file will have a space in font of each field, so excel will understand it as text.
So you create a .vbs file with the code below (for example Modify_CSV.vbs), save and close it. Drag and Drop your original file to your vbscript file. It will create a new file with "SPACE_ADDED" to file name in the same location.
Set objArgs = WScript.Arguments
Set objFso = createobject("scripting.filesystemobject")
dim objTextFile
dim arrStr ' an array to hold the text content
dim sLine ' holding text to write to new file
'Looping through all dropped file
For t = 0 to objArgs.Count - 1
' Input Path
inPath = objFso.GetFile(wscript.arguments.item(t))
' OutPut Path
outPath = replace(inPath, objFso.GetFileName(inPath), left(objFso.GetFileName(inPath), InStrRev(objFso.GetFileName(inPath),".") - 1) & "_SPACE_ADDED.csv")
' Read the file
set objTextFile = objFso.OpenTextFile(inPath)
'Now Creating the file can overwrite exiting file
set aNewFile = objFso.CreateTextFile(outPath, True)
aNewFile.Close
'Open the file to appending data
set aNewFile = objFso.OpenTextFile(outPath, 8) '2=Open for writing 8 for appending
' Reading data and writing it to new file
Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,",")
sLine = "" 'Clear previous data
For i=lbound(arrStr) to ubound(arrStr)
sLine = sLine + " " + arrStr(i) + ","
Next
'Writing data to new file
aNewFile.WriteLine left(sLine, len(sLine)-1) 'Get rid of that extra comma from the loop
Loop
'Closing new file
aNewFile.Close
Next ' This is for next file
set aNewFile=nothing
set objFso = nothing
set objArgs = nothing
Its not the Excel. Windows does recognize the formula, the data as a date and autocorrects. You have to change the Windows settings.
"Control Panel" (-> "Switch to Classic View") -> "Regional and Language
Options" -> tab "Regional Options" -> "Customize..." -> tab "Numbers" -> And
then change the symbols according to what you want.
http://www.pcreview.co.uk/forums/enable-disable-auto-convert-number-date-t3791902.html
It will work on your computer, if these settings are not changed for example on your customers' computer they will see dates instead of data.
Without modifying your csv file you can:
Change the excel Format Cells option to "text"
Then using the "Text Import Wizard" to define the csv cells.
Once imported delete that data
then just paste as plain text
excel will properly format and separate your csv cells as text formatted ignoring auto date formats.
Kind of a silly work around, but it beats modifying the csv data before importing. Andy Baird and Richard sort of eluded to this method, but missed a couple important steps.
In my case, "Sept8" in a csv file generated using R was converted into "8-Sept" by Excel 2013. The problem was solved by using write.xlsx2() function in the xlsx package to generate the output file in xlsx format, which can be loaded by Excel without unwanted conversion. So, if you are given a csv file, you can try loading it into R and converting it into xlsx using the write.xlsx2() function.
EASIEST SOLUTION
I just figured this out today.
Open in Word
Replace all hyphens with en dashes
Save and Close
Open in Excel
Once you are done editing, you can always open it back up in Word again to replace the en dashes with hyphens again.
A workaround using Google Drive (or Numbers if you're on a Mac):
Open the data in Excel
Set the format of the column with incorrect data to Text (Format > Cells > Number > Text)
Load the .csv into Google Drive, and open it with Google Sheets
Copy the offending column
Paste column into Excel as Text (Edit > Paste Special > Text)
Alternatively if you're on a Mac for step 3 you can open the data in Numbers.
(EXCEL 2016 and later, actually I have not tried in older versions)
Open new blank page
Go to tab "Data"
Click "From Text/CSV" and choose your csv file
Check in preview whether your data is correct.
In сase when some column is converted to date click "edit" and then select type Text by clicking on calendar in head of column
Click "Close & Load"
If someone still looking for answer, the line below worked perfectly for me
I entered =("my_value").
i.e. =("04SEP2009") displayed as 04SEP2009 not as 09/04/2009
The same worked for integers more than 15 digits. They weren't getting trimmed anymore.
If you can change the file source data
If you're prepared to alter the original source CSV file, another option is to change the 'delimiter' in the data, so if your data is '4/11' (or 4-11) and Excel converts this to 4/11/2021 (UK or 11-4-2021 US), then changing the '/' or '-' character to something else will thwart the unwantwed Excel date conversion. Options may include:
Tilde ('~')
Plus ('+')
Underscore ('_')
Double-dash ('--')
En-dash (Alt 150)
Em-dash (Alt 151)
(Some other character!)
Note: moving to Unicode or other non-ascii/ansi characters may complicate matters if the file is to be used elsewhere.
So, '4-11' converted to '4~11' with a tilde will NOT be treated as a date!
For large CSV files, this has no additional overhead (ie: extra quotes/spaces/tabs/formula constructs) and just works when the file is opened directly (ie: double-clicking the CSV to open) and avoids pre-formatting columns as text or 'importing' the CSV file as text.
A search/replace in Notepad (or similar tool) can easily convert to/from the alternative delimiter, if necessary.
Import the original data
In newer versions of Excel you can import the data (outlined in other answers).
In older versions of Excel, you can install the 'Power Query' add-in. This tool can also import CSVs without conversion. Choose: Power Query tab/From file/From Text-CSV, then 'Load' to open as a table. (You can choose 'do not detect data types' from the 'data type detection' options).
I know this is an old thread. For the ones like me, who still have this problem using Office 2013 via PowerShell COM object can use the opentext method. The problem is that this method has many arguments, that are sometimes mutual exclusive. To resolve this issue you can use the invoke-namedparameter method introduced in this post.
An example would be
$ex = New-Object -com "Excel.Application"
$ex.visible = $true
$csv = "path\to\your\csv.csv"
Invoke-NamedParameter ($ex.workbooks) "opentext" #{"filename"=$csv; "Semicolon"= $true}
Unfortunately I just discovered that this method somehow breaks the CSV parsing when cells contain line breaks. This is supported by CSV but Microsoft's implementation seems to be bugged.
Also it did somehow not detect German-specific chars. Giving it the correct culture did not change this behaviour. All files (CSV and script) are saved with utf8 encoding.
First I wrote the following code to insert the CSV cell by cell.
$ex = New-Object -com "Excel.Application"
$ex.visible = $true;
$csv = "path\to\your\csv.csv";
$ex.workbooks.add();
$ex.activeWorkbook.activeSheet.Cells.NumberFormat = "#";
$data = import-csv $csv -encoding utf8 -delimiter ";";
$row = 1;
$data | %{ $obj = $_; $col = 1; $_.psobject.properties.Name |%{if($row -eq1){$ex.ActiveWorkbook.activeSheet.Cells.item($row,$col).Value2= $_ };$ex.ActiveWorkbook.activeSheet.Cells.item($row+1,$col).Value2 =$obj.$_; $col++ }; $row++;}
But this is extremely slow, which is why I looked for an alternative. Apparently, Excel allows you to set the values of a range of cells with a matrix. So I used the algorithm in this blog to transform the CSV in a multiarray.
function csvToExcel($csv,$delimiter){
$a = New-Object -com "Excel.Application"
$a.visible = $true
$a.workbooks.add()
$a.activeWorkbook.activeSheet.Cells.NumberFormat = "#"
$data = import-csv -delimiter $delimiter $csv;
$array = ($data |ConvertTo-MultiArray).Value
$starta = [int][char]'a' - 1
if ($array.GetLength(1) -gt 26) {
$col = [char]([int][math]::Floor($array.GetLength(1)/26) + $starta) + [char](($array.GetLength(1)%26) + $Starta)
} else {
$col = [char]($array.GetLength(1) + $starta)
}
$range = $a.activeWorkbook.activeSheet.Range("a1:"+$col+""+$array.GetLength(0))
$range.value2 = $array;
$range.Columns.AutoFit();
$range.Rows.AutoFit();
$range.Cells.HorizontalAlignment = -4131
$range.Cells.VerticalAlignment = -4160
}
function ConvertTo-MultiArray {
param(
[Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true)]
[PSObject[]]$InputObject
)
BEGIN {
$objects = #()
[ref]$array = [ref]$null
}
Process {
$objects += $InputObject
}
END {
$properties = $objects[0].psobject.properties |%{$_.name}
$array.Value = New-Object 'object[,]' ($objects.Count+1),$properties.count
# i = row and j = column
$j = 0
$properties |%{
$array.Value[0,$j] = $_.tostring()
$j++
}
$i = 1
$objects |% {
$item = $_
$j = 0
$properties | % {
if ($item.($_) -eq $null) {
$array.value[$i,$j] = ""
}
else {
$array.value[$i,$j] = $item.($_).tostring()
}
$j++
}
$i++
}
$array
}
}
csvToExcel "storage_stats.csv" ";"
You can use above code as is; it should convert any CSV into Excel. Just change the path to the CSV and the delimiter character at the bottom.
Okay found a simple way to do this in Excel 2003 through 2007. Open a blank xls workbook. Then go to Data menu, import external data. Select your csv file. Go through the wizard and then in "column data format" select any column that needs to be forced to "text". This will import that entire column as a text format preventing Excel from trying to treat any specific cells as a date.
This issue is still present in Mac Office 2011 and Office 2013, I cannot prevent it happening. It seems such a basic thing.
In my case I had values such as "1 - 2" & "7 - 12" within the CSV enclosed correctly within inverted commas, this automatically converts to a date within excel, if you try subsequently convert it to just plain text you would get a number representation of the date such as 43768. Additionally it reformats large numbers found in barcodes and EAN numbers to 123E+ numbers again which cannot be converted back.
I have found that Google Drive's Google Sheets doesnt convert the numbers to dates. The barcodes do have commas in them every 3 characters but these are easily removed. It handles CSVs really well especially when dealing with MAC / Windows CSVs.
Might save someone sometime.
I do this for credit card numbers which keep converting to scientific notation: I end up importing my .csv into Google Sheets. The import options now allow to disable automatic formatting of numeric values. I set any sensitive columns to Plain Text and download as xlsx.
It's a terrible workflow, but at least my values are left the way they should be.
I made this VBA macro which basically formats the output range as text before pasting the numbers. It works perfectly for me when I want to paste values such as 8/11, 23/6, 1/3, etc. without Excel interpreting them as dates.
Sub PasteAsText()
' Created by Lars-Erik Sørbotten, 2017-09-17
Call CreateSheetBackup
Columns(ActiveCell.Column).NumberFormat = "#"
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
ActiveCell.PasteSpecial
End Sub
I'm very interested in knowing if this works for other people as well. I've been looking for a solution to this problem for a while, but I haven't seen a quick VBA solution to it that didn't include inserting ' in front of the input text. This code retains the data in its original form.

Excel CSV - Number cell format

I produce a report as an CSV file.
When I try to open the file in Excel, it makes an assumption about the data type based on the contents of the cell, and reformats it accordingly.
For example, if the CSV file contains
...,005,...
Then Excel shows it as 5.
Is there a way to override this and display 005?
I would prefer to do something to the file itself, so that the user could just double-click on the CSV file to open it.
I use Excel 2003.
There isn’t an easy way to control the formatting Excel applies when opening a .csv file. However listed below are three approaches that might help.
My preference is the first option.
Option 1 – Change the data in the file
You could change the data in the .csv file as follows ...,=”005”,...
This will be displayed in Excel as ...,005,...
Excel will have kept the data as a formula, but copying the column and using paste special values will get rid of the formula but retain the formatting
Option 2 – Format the data
If it is simply a format issue and all your data in that column has a three digits length. Then open the data in Excel and then format the column containing the data with this custom format 000
Option 3 – Change the file extension to .dif (Data interchange format)
Change the file extension and use the file import wizard to control the formats.
Files with a .dif extension are automatically opened by Excel when double clicked on.
Step by step:
Change the file extension from .csv to .dif
Double click on the file to open it in Excel.
The 'File Import Wizard' will be launched.
Set the 'File type' to 'Delimited' and click on the 'Next' button.
Under Delimiters, tick 'Comma' and click on the 'Next' button.
Click on each column of your data that is displayed and select a 'Column data format'. The column with the value '005' should be formatted as 'Text'.
Click on the finish button, the file will be opened by Excel with the formats that you have specified.
Don't use CSV, use SYLK.
http://en.wikipedia.org/wiki/SYmbolic_LinK_(SYLK)
It gives much more control over formatting, and Excel won't try to guess the type of a field by examining the contents. It looks a bit complicated, but you can get away with using a very small subset.
This works for Microsoft Office 2010, Excel Version 14
I misread the OP's preference "to do something to the file itself." I'm still keeping this for those who want a solution to format the import directly
Open a blank (new) file (File -> New from workbook)
Open the Import Wizard (Data -> From Text)
Select your .csv file and Import
In the dialogue box, choose 'Delimited', and click Next.
Choose your delimiters (uncheck everything but 'comma'), choose your Text qualifiers (likely {None}), click Next
In the Data preview field select the column you want to be text. It should highlight.
In the Column data format field, select 'Text'.
Click finished.
You can simply format your range as Text.
Also here is a nice article on the number formats and how you can program them.
Actually I discovered that, at least starting with Office 2003, you can save an Excel spreadsheet as an XML file.
Thus, I can produce an XML file and when I double-click on it, it'll be opened in Excel.
It provides the same level of control as SYLK, but XML syntax is more intuitive.
Adding a non-breaking space in the cell could help.
For instance:
"firstvalue";"secondvalue";"005 ";"othervalue"
It forces Excel to treat it as a text and the space is not visible.
On Windows you can add a non-breaking space by tiping alt+0160.
See here for more info: http://en.wikipedia.org/wiki/Non-breaking_space
Tried on Excel 2010.
Hope this can help people who still search a quite proper solution for this problem.
I had this issue when exporting CSV data from C# code, and resolved this by prepending the leading zero data with the tab character \t, so the data was interpreted as text rather than numeric in Excel (yet unlike prepending other characters, it wouldn't be seen).
I did like the ="001" approach, but this wouldn't allow exported CSV data to be re-imported again to my C# application without removing all this formatting from the import CSV file (instead I'll just trim the import data).
I believe when you import the file you can select the Column Type. Make it Text instead of Number. I don't have a copy in front of me at the moment to check though.
Load csv into oleDB and force all inferred datatypes to string
i asked the same question and then answerd it with code.
basically when the csv file is loaded the oledb driver makes assumptions, you can tell it what assumptions to make.
My code forces all datatypes to string though ... its very easy to change the schema.
for my purposes i used an xslt to get ti the way i wanted - but i am parsing a wide variety of files.
I know this is an old question, but I have a solution that isn't listed here.
When you produce the csv add a space after the comma but before your value e.g. , 005,.
This worked to prevent auto date formatting in excel 2007 anyway .
The Text Import Wizard method does NOT work when the CSV file being imported has line breaks within a cell. This method handles this scenario(at least with tab delimited data):
Create new Excel file
Ctrl+A to select all cells
In Number Format combobox, select Text
Open tab delimited file in text editor
Select all, copy and paste into Excel
Just add ' before the number in the CSV doc.
This has been driving me crazy all day (since indeed you can't control the Excel column types before opening the CSV file), and this worked for me, using VB.NET and Excel Interop:
'Convert .csv file to .txt file.
FileName = ConvertToText(FileName)
Dim ColumnTypes(,) As Integer = New Integer(,) {{1, xlTextFormat}, _
{2, xlTextFormat}, _
{3, xlGeneralFormat}, _
{4, xlGeneralFormat}, _
{5, xlGeneralFormat}, _
{6, xlGeneralFormat}}
'We are using OpenText() in order to specify the column types.
mxlApp.Workbooks.OpenText(FileName, , , Excel.XlTextParsingType.xlDelimited, , , True, , True, , , , ColumnTypes)
mxlWorkBook = mxlApp.ActiveWorkbook
mxlWorkSheet = CType(mxlApp.ActiveSheet, Excel.Worksheet)
Private Function ConvertToText(ByVal FileName As String) As String
'Convert the .csv file to a .txt file.
'If the file is a text file, we can specify the column types.
'Otherwise, the Codes are first converted to numbers, which loses trailing zeros.
Try
Dim MyReader As New StreamReader(FileName)
Dim NewFileName As String = FileName.Replace(".CSV", ".TXT")
Dim MyWriter As New StreamWriter(NewFileName, False)
Dim strLine As String
Do While Not MyReader.EndOfStream
strLine = MyReader.ReadLine
MyWriter.WriteLine(strLine)
Loop
MyReader.Close()
MyReader.Dispose()
MyWriter.Close()
MyWriter.Dispose()
Return NewFileName
Catch ex As Exception
MsgBox(ex.Message)
Return ""
End Try
End Function
When opening a CSV, you get the text import wizard. At the last step of the wizard, you should be able to import the specific column as text, thereby retaining the '00' prefix. After that you can then format the cell any way that you want.
I tried with with Excel 2007 and it appeared to work.
Well, excel never pops up the wizard for CSV files. If you rename it to .txt, you'll see the wizard when you do a File>Open in Excel the next time.
Put a single quote before the field. Excel will treat it as text, even if it looks like a number.
...,`005,...
EDIT: This is wrong. The apostrophe trick only works when entering data directly into Excel. When you use it in a CSV file, the apostrophe appears in the field, which you don't want.
http://support.microsoft.com/kb/214233

Resources