VBA equivalent of DATEVALUE? - excel

The formula function of =DATEVALUE and VBA's DateValue() provide exclusive results. How can I format a date in VBA to obtain the same serialization as I would by using =DATEVALUE?
Example: 1/19/2016, formatted "m/d/yyyy", will return 42388 through =DATEVALUE()

Use CLng e.g.
=DATEVALUE("7/7/16")
Returns
42558
In VBA:
? CLng(DateValue("7/7/16"))
42558

Several ways to do this. #Robin has a good one. I'll add one more
?DateDiff("d",0, "7/7/16")
42558
The DateDiff function calculates the difference between the two dates (2nd and 3rd parameters) in the units of the first parameter. So if you changed "d" to "h" you would get the number of hours between 0 (the first possible date in Excel/VBA) and 7/7/2016

Related

Issue with DATEDIF function

DATEDIF function giving different values as shown in the snapshot.
Why this?
First of all, you should avoid passing D2 directly to DATEDIF formula.
DATEDIF requires first 2 arguments as a date. You pass 0 and D2 (C2-B2). Since lowest possible date in excel is 01-Jan-1900 and it is = 1, your arguments are 0 = 0-Jan-1900 (non existing date) and 546 = 29-Jun-1900. You would get more errors in calculation if your dates would include leap year (1900 is not leap year)
Also consider looking at Microsoft as it states there are issues with "md" argument (see "Known issues").
So correct formula would be:
=DATEDIF(B2,C2,"y")&" years "&DATEDIF(B2,C2,"ym")&" months "&DATEDIF(B2,C2,"md")&" days"
Result:

Formatting date in Excel

How can I easily format "202104" --> 2021/04 in Excel?
My current method is to concatenate the original string with "01" and then change it into a date. However, I am seeking a more efficient format method.
Thanks
Try this.
For Text
=LEFT(A1,4)&"/"&RIGHT(A1,2)
For Date Value
=TEXT(DATEVALUE(LEFT(A1,4)&"/"&RIGHT(A1,2)),"YYYY/MM")
As far as I know, 2021/04 is not a valid date in Excel, but 2021/04/01 (first of April, year 2021) is.
In order to achieve this, you can use this formula:
=DATE(INTEGER(202104 / 100);MOD(202104;100);1)
Where:
1) INTEGER(202104/100) is the integer division of 202104 by 100, calculating the year.
2) MOD(202104;100) means 202104 modulo 100, in order to calculate the month.
3) 1 means the first day of the month.

use IF() with TIMEVALUE() in excel

I have the folowing formula:
=IF(TIMEVALUE("14:30") - TIMEVALUE(NOW()) < TIMEVALUE("00:00"),"Past","Future")
Excel is giving me ERROR, i checked all formulas individually and they all give me the time value (which in theory should be enough to compare with an IF statement).
How come that i keep on getting error. Some cell formats not correct or something? Any help is appreciated!
Try to evaluate the formula step by step to see, if this is what you want.
Working formula:
=IF(TIMEVALUE("14:30") - NOW() < TIMEVALUE("00:00");"Past";"Future")
Evaluated arguments, using [F9 key]:
=IF(0,604166666666667 - 42719,6943635416 < 0;"Past";"Future")
You will always get the "Past" as returned value.
Explanation of date and time in Excel
The TIME in Excel is a proportion of a day. "00:00" = 0.00 and "24:00" = 1.00. Other values of TIME are DECIMALS between 0 and 1.
The DATE is a number of days since the first day. "1900-01-01" = 1 and "2000-01-01" = 36526. It is always an INTEGER.
Combining DATA and TIME (like in NOW() function) gives you an INTEGER + DECIMAL. When I evaluated the NOW() it returned 42719,6943635416
The TIMEVALUE function expects a STRING/TEXT that is considered a TIME and converts that STRING/TEXT to DECIMAL between <0; 1>.
Links
MS - How to use dates and times in Excel
Trump-Excel:Identify Errors Using Excel Formula Debugging (2 Methods)
Convert the Now to string first, using Text function, then it will work as expected.
TEXT(NOW(),"HH:MM:SS")
Final formula:
=IF((TIMEVALUE("11:30") - TIMEVALUE(TEXT(NOW(),"HH:MM:SS"))) < TIMEVALUE("00:00"),"Past","Future")

Show only the year (no century) for select dates in the X axis

I'm showing only certain dates in my X axis and I'm selecting these dates from my "dataPop" array with the tickValues function:
xAxisPop.tickValues([dataPop[0].date, dataPop[20].date, dataPop[40].date])
however, I'd like dataPop[20].date and dataPop[40].date to only show the year and not the century, for example, 1980 as 80.
I have a variable "formatDate" written to show only the year and not century:
formatDate = d3.time.format("%y")
but how do I apply formatDate to dataPop[20].date, dataPop[40].date in the tickValues function?
Also, fyi. My dates are currently strings... so I suppose instead of using d3.time.format in my formatDate function I should be using a different D3 formatting method that formats strings...is that correct? or should I be changing the dates from strings to dates first, and then running the formatDate function?
thanks much
You'll pass the formatter in to xAxisPop.tickFormat. Any d3 text formatting option like tickFormat takes a regular JavaScript function as a parameter. d3.time.format is just a helper method to construct the common ones.
In your case, since your dates are just strings, I'd recommend just writing your own formatting function, perhaps:
function formatTwoDigitYear(d) { return d.substr(2,2); }
That being said, you'd probably see a number of advantages by converting the textual years to dates as early as you can.

How to convert and compare a date string to a date in Excel

= "7/29/2011 12:58:00 PM" > NOW()
I'd like this expression to return FALSE and yet it returns TRUE.
I know I can break apart my datetime into a date and a time and add them together as follows:
= DateValue("7/29/2011") + TimeValue("12:58:00 PM") > NOW()
But, this seems inelegant to me. I want a simple function or approach that looks nice and I feel certain that it's out there but I just can't find it.
I also know there is a VBA function called CDate which can typecast the string into a datetime and that would be perfect. But, I don't see how to call a VBA function in an excel cell.
Multiply the string by one and the comparison function will work:
= 1*"7/29/2011 12:58:00 PM" > NOW()
The answer to your question is tightly related to #Jean-François's comment: Why is the date being interpreted by Excel as a Text and not by a date?
Once you find it out, you'll be able to do the comparison.
If that's because the string is being retrieved as a text, you can simply multiply it by one and the comparison function will work, then. But it applies only in case the string format is a valid date/time format in your regional settings.
You could wrap the VBA call in a custom function:
Function ReturnDate(ByVal datestr As String) As Date
ReturnDate = CDate(datestr)
End Function
which you can use just like a formula in your sheet.
I'm upgrading the following from a comment to an answer:
Unless you have a very specific reason to do so (and right now I can't think of any), dates (and other values) really shouldn't be "hard-coded" in cells as strings like you show. Hard-coding the string like that makes it invisible and inflexible. The user will just see TRUE or FALSE with no indication of what this means.
I would just put your date 7/29/2011 12:58:00 PM in a cell on its own e.g. A1, and set the cell's format to some date format. Then you can say = A1 > NOW().
Contrary to #jonsca's and #Tiago Cardoso's answers, this answer doesn't address your specific question, but then again, what you are asking seems like really bad practice to me!
The simplest way to do this is to make a VBA function that uses CDATE and return your comparison. Then, call the function from an excel cell.
The VBA Function
Public Function compareDate(ByVal inputDate As String) As Boolean
compareDate = CDate(inputDate) > Now()
End Function
Then in your spreadsheet, just do
=compareDate("YOUR DATE")
The function will return "FALSE" if it is older and "TRUE" if it is newer than Now()

Resources