Sashka
Sashka

Reputation: 53

Excel #Value error when using DATEVALUE function

In cell A2 I have 7/21/2014 12:44:36 PM

When I use DATEVALUE(LEFT(A2;FIND(" ";A2)-1)) I get the error #VALUE.

When I use LEFT(A2;FIND(" ";A2)-1) I get 7/21/2014.

What do I need do that function DATEVALUE(LEFT(A2;FIND(" ";A2)-1)) to return just the date?

Upvotes: 5

Views: 47398

Answers (4)

Shawn H
Shawn H

Reputation: 11

SIMPLE SOLUTION - I found a solve that worked very well:

=DATEVALUE(TEXT([CELL],"MM/DD/YYYY")

Effectively this lets me convert any value into text, then back into date. It fixed my Datevalue error and I can use it regardless of the original cell formatting.

Upvotes: 1

Shadaksharayya H A
Shadaksharayya H A

Reputation: 127

Date macros are depending on the system date format and based on configured system date format the macros can fail to work. Try below solution. We faced similar issue after open excel file sent by me on another laptop where system date/time format was different. On destination laptop formulas using date functions started giving error. After following below steps errors disappeared.

Left click on bottom right portion of task bar where time is displayed Click on date and time settings Click on change date and time Change calendar settings Click on reset to go back to original values Click on OK on all opened dialogues Now excel formula error should disappear

Upvotes: 0

StephenH
StephenH

Reputation: 1126

DATEVALUE() is designed to make a Date out of plain text. Your cell is currently a Date/Time, which is a numeric value. I recommend using one of the following solutions to get the date from the cell.

Using DATE()


This is the cleanest option and the method that I would recommend.

=DATE(YEAR(A2),MONTH(A2),DAY(A2))

YEAR() gets the Year value from the cell, MONTH() gets the Month value, and DAY() gets the Day value. The DATE() function takes a Year, Month, and Day value, so by passing them into DATE() we can get the Date value from A2.

Using INT()


If we look at the numeric value of your Date in A2, we see that it is 41841.5309722222. The whole portion of the number (41841.) is the date and the decimal portion (.5309722222) is the time. So if we take INT(A2) to convert this value to an integer, we will lose the decimal portion so that all that remains (41841) is the date. So this is our formula for using INT()

=INT(A2)

The same idea can be accomplished with ROUNDDOWN(A2,0) or =FLOOR.MATH(A2) or =FLOOR(A2,1).

Using DATEVALUE()


While the first solution is the cleanest, there is a way to do this with DATEVALUE() that involves converting the cell into Text first. The TEXT() function takes a value and a format string, so we format the cell value as Text as follows

=TEXT(A2,"yyyy-mm-dd")

And this gives us

2014-07-21

We then pass that result into DATEVALUE()

=DATEVALUE(TEXT(A2,"yyyy-mm-dd"))

You will need to format the result as a date.

Using LEFT() and DATEVALUE()


Based on this Stackoverflow question that I found, it appears the issue could be a result of inconsistent formatting, so you might try this solution

=DATEVALUE(LEFT(A2,FIND(" ",A2)-1))

I have included my results with this and the other methods in a screenshot below. You can see by my use of the TYPE() command below the value that I tested this on both a number and text.

Results

Results of formulas on different value types

Formatting


I'm assuming you want just the date for calculation purposes, but if you just want to display the date, you can format the cell to only show the date and ignore the time element although the time element will still be present in the cell. I'm assuming you know about this, but since you didn't specify, it is a possible solution.

Upvotes: 12

pnuts
pnuts

Reputation: 59485

Please try:

 =INT(A2)

and format the result to suit.

In a comment I have just seen you mention "=INT(A2) return #VALUE!" so I would suggest selecting your date cells, DATA > Data Tools, - Text to Columns, Delimited, Delimiters Tab (only), and at Step 3 of 3 choose MDY for Date: or change your locale to say USA.

If neither work try =INT(TRIM(A2)) in case you have leading spaces (though not showing them).

If still not working, try applying =CLEAN.

If still nothing works then some further details of where your dates are coming from and how imported would be helpful, and of your locale and default date format.

Upvotes: 3

Related Questions