简体   繁体   中英

Excel - Calculate date closest to system date

I have 4 fields that holds date information that are updated periodically, I need to add a 5th field that does the following for reporting in a pivot table:

Checks if all the fields are blank, if not calculate the field value (a date) that is closest to the system date(current date), I have the first part but the second part of the if statement has me stumped.

IF(AND(GT14="",GU14="",GV14="",GW14="",GX14=""),"No Tollgate Dates",if statement to field that is closest to most recent month)

Any help would be greatly appreciated.

This should do the trick:

=IF(SUM(GT14:GX14)=0,"No Tollgate Dates",INDEX(GT14:GX14,MATCH(SMALL(ABS(TODAY()-(GT14:GX14)),1),ABS(TODAY()-(GT14:GX14)),0)))

It is an array-formula, so you must use ctrl+shift+enter.

You could use the ABS function as Ron Rosenfeld says, either in a helper row to get the absolute differences and find the date corresponding to the smallest one, or using an array formula. There could be two solutions, eg if today is 22/8/16, 21/8/16 and 23/8/16 would both be correct if all other dates are further away.

=MIN(IF(ABS(GT14:GX14-TODAY())=MIN(ABS(GT14:GX14-TODAY())),GT14:GX14))

to find the earlier one

=MAX(IF(ABS(GT14:GX14-TODAY())=MIN(ABS(GT14:GX14-TODAY())),GT14:GX14))

to find the later one.

You could put this formula inside your existing formula, but as mentioned it could be a bit long.

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