简体   繁体   中英

DATEDIF and IF Statement in Excel, won't return correct result over 10 months

I am attempting to determine the number of months and days between two dates to determine an group of individuals length of service, so that I can ascertain if they are within 2 ranges 0-6 mths and 6-23 mths, I have the following but I'm missing something, as it can't seem to handle 10 months or over, its returning 0-6 mths for those. I will also need to add a third range (0-6, 6-12 & 12-23) for a future project but am having difficulties with this one also?

=IF(DATEDIF(F155,G155,"ym")&" months " &DATEDIF(F155,G155,"md")&" days">="6 months 0 days", "6 - 23 Months","0 - 6 Months")

The problem is that you are comparing 2 strings that do not have the same format: For example for dates 1/1/2019 with 11/1/2019 you are comparing "10 months 0 days" vs "6 months 0 days" (a 2 digits number vs a 1 digit number in the begining strings). You have to make them the same format to be able to compare:

=IF(DATEDIF(F155,G155,"ym")&" months " &RIGHT("0"&DATEDIF(F155,G155,"md"),2)&" days">="06 months 00 days", "6 - 23 Months","0 - 6 Months")

This way you would be comparing "10 months 00 days" vs "06 months 00 days" and since now they have the same format it will work.

It might be simpler to use VLOOKUP .

And, if you expect to get a range of 12-23 months , why are you using the "ym" argument for DATEDIF ? That can never return a value more than 12 .

I suggest something like:

=VLOOKUP(DATEDIF(F155,G155,"m"),{0,"0 to 6 months";6,"6 to 12 months";12,"12 to 23 months";24,"undefined"},2)

Also, suggest you read about the different arguments for DATEDIF ; and you should probably read HELP for VLOOKUP also.

If you need to extend the table more, consider putting it into an Excel table instead of an array constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM