简体   繁体   English

Excel公式在最接近或相等的日期返回值

[英]Excel Formula to Return Value on Closest or Equal Date

I'm working on an excel formula that will reference a table on a different worksheet in the same workbook and return a value based on the current date. 我正在研究一个Excel公式,该公式将引用同一工作簿中不同工作表上的表,并根据当前日期返回一个值。 Thus far I have been able to use this formula to return the value as I am intending. 到目前为止,我已经能够使用该公式按预期返回值。 I've included an example table for the formula to show the methodology. 我为该公式提供了一个示例表,以显示该方法。

https://i.stack.imgur.com/ThwL0.png https://i.stack.imgur.com/ThwL0.png

=VLOOKUP(TODAY(),TRANSACTIONS!A:D,4,FALSE)

While this works, the flaw is that it will return #N/A if there are no lines which have an exact matching date. 虽然这样做有效,但缺点是,如果没有行具有完全匹配的日期,它将返回#N / A。 In the case of the above table, the transactions don't occur daily so if the given date returned by TODAY() doesn't match any line in the shown table, it breaks the formula. 在上表的情况下,交易不是每天进行的,因此,如果TODAY()返回的给定日期与所示表中的任何行都不匹配,则会中断公式。 My table also contains "planned" or "expected" transactions which exist into the future meaning I can't rely on the MAX() function to provide the correct result 我的表还包含将来存在的“计划的”或“预期的”事务,这意味着我不能依靠MAX()函数来提供正确的结果

With that said, I am looking for a tweak to this formula that will always return a value even if the current date doesn't match any rows exactly. 话虽如此,我正在寻找对此公式的调整,即使当前日期与任何行都不完全匹配,该公式也将始终返回值。 Using the above image as a reference, if the date returned by TODAY() were to be 2/20/17, the result it should give should be the value in D5, 1566. Conversely if the date is 2/28/17, it should return the value in D7, 1182. 使用上图作为参考,如果TODAY()返回的日期为2/20/17,则其给出的结果应为D5 1566中的值。相反,如果日期为2/28/17,它应返回D7 1182中的值。

Open to any an all suggestions which will achieve these results. 向所有人开放将获得这些结果的所有建议。 Let me know if you need any further clarification. 让我知道是否需要进一步说明。 Thanks! 谢谢!

I'd use 我会用

=IFERROR(VLOOKUP(E2,A:D,4,TRUE),D2)

so that should your date fall before the lowest in column A it'd return the first value of column D (ie the value in D2 cell) 因此,如果您的日期早于A列中的最低值,它将返回D列的第一个值(即D2单元格中的值)

在上面已经建议的IFERROR中,使用INDEX MATCH而不是VLOOKUP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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