Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Text to Date?

Tags:

excel

vba

I have a column of dates (column A) stored as text in the format yyyy-mm-dd which I'm trying to convert to dates, ultimately so that I can do look ups against them.

I've read a few topics on here and tried some of the suggestions, but I can't get anything to work. One was using:

Columns("A").Select Selection.NumberFormat = "date" 

This changed the format of the cells to date but didn't actually change the format of the value which was still stored as text.

My understanding is that I need to use CDate() to change the format of the value from text to date. I've tried something like this:

Dim c As Range For Each c In ActiveSheet.UsedRange.columns("A").Cells     c.Value = CDate(c.Value) Next c 

Which gives me a type mismatch error. I wondered if this was because I was trying to save date values back into a non-date formatted cell so I tried combining it with the .NumberFormat = "date" above, which didn't work either.

Any suggestions would be appreciated.

like image 533
user1300244 Avatar asked Dec 04 '13 12:12

user1300244


People also ask

How do I convert plain text to date?

The DATEVALUE function in Excel converts a date in the text format to a serial number that Excel recognizes as a date. So, the formula to convert a text value to date is as simple as =DATEVALUE(A1) , where A1 is a cell with a date stored as a text string.

How do you convert general text to date format in Excel?

Select the cell or cells with serial numbers that you want to convert to dates. Locate the "Home" tab in the top right of the Excel window and click on it. Find the "Number Format" box in the toolbar that will usually say "General" or "Text." Click the down arrow next to the box and select "Date" from the menu.

How do I change the date format from text to dd mm yyyy in Excel?

To change the date display in Excel follow these steps: Go to Format Cells > Custom. Enter dd/mm/yyyy in the available space.


1 Answers

You can use DateValue to convert your string to a date in this instance

Dim c As Range For Each c In ActiveSheet.UsedRange.columns("A").Cells     c.Value = DateValue(c.Value) Next c 

It can convert yyyy-mm-dd format string directly into a native Excel date value.

like image 200
Sam Avatar answered Sep 28 '22 17:09

Sam