Stop Excel from automatically converting certain text values to dates

Viewed 590455

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.

36 Answers

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.

2018

The only proper solution that worked for me (and also without modifying the CSV).

Excel 2010:

  1. Create new workbook
  2. Data > From Text > Select your CSV file
  3. In the popup, choose "Delimited" radio button, then click "Next >"
  4. Delimiters checkboxes: tick only "Comma" and uncheck the other options, then click "Next >"
  5. 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)

  1. Create new workbook
  2. Data > From Text/CSV > Select your CSV file
  3. Data type detection > do not detect

Note: Excel office365 (web version), as I'm writing this, you will not be able to do that.

(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."

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.

(EXCEL 2016 and later, actually I have not tried in older versions)

  1. Open new blank page
  2. Go to tab "Data"
  3. Click "From Text/CSV" and choose your csv file
  4. Check in preview whether your data is correct.
  5. In сase when some column is converted to date click "edit" and then select type Text by clicking on calendar in head of column
  6. Click "Close & Load"

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).

If you put an inverted comma at the start of the field, it will be interpreted as text.

Example: 25/12/2008 becomes '25/12/2008

You are also able to select the field type when importing.

An alternate method:

Convert the format of the column you want to change to 'Text'. Select all the cells you want to preserve, copy. Without deselecting those columns, click "Edit > Paste Special > As values"

Save as CSV. Note that this has to be the last thing you do to the file because when you reopen it, it will format itself as dates since cell formats cannot be saved in CSV files.

Related