返回年末日期的功能
我有许多财务报告,并非总是在12月31日结束。
我正在努力的功能应为年终日期,并返回现在日期和下一年末日期之间的天数,除以365。前瞻性,因此,如果年终日期是昨天,则该功能应返回364天,而不是1。)
以下尝试回报1。我相信这与约会夫有关。
Function dateCalc(ye As Date)
Dim today As Date
Dim x As Integer
today = Date
If ye < today Then
x = (365 - DateDiff("d", ye, today)) / 365
ElseIf ye > today Then
x = DateDiff("d", ye, today) / 365
Else
x = 0
End If
dateCalc = x
End Function
I have many financial reports, not always ending on Dec 31.
The function I'm working on should take the year-end date and return the number of days between the present date and the next year-end date, divided by 365. (Forward-looking, so if the year-end date was yesterday then the function should return 364 days, rather than 1.)
The attempt below returns 1. I believe it has to do with DateDiff.
Function dateCalc(ye As Date)
Dim today As Date
Dim x As Integer
today = Date
If ye < today Then
x = (365 - DateDiff("d", ye, today)) / 365
ElseIf ye > today Then
x = DateDiff("d", ye, today) / 365
Else
x = 0
End If
dateCalc = x
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用代码的以下衍生物,
在6/14/2022日期,我们看到以下内容:
?DateCalc(#6/15/2022#)
生产-0.00273972602739726
?datecalc(#6/13/2022#)
产生0.997260273972603
请注意,在
ye&gt;今天
案例,约会夫为-1。如果您想要积极的话,请否定它。从问题中尚不清楚这是否是所需的输出,但是主要问题似乎是使用Integer
而不是double> double
。Using the below derivative of your code,
On the date 6/14/2022, we see the following:
?dateCalc(#6/15/2022#)
produces-0.00273972602739726
?dateCalc(#6/13/2022#)
produces0.997260273972603
Note that in the
YE > Today
case, the DateDiff is -1. If you wanted a positive, negate it. It wasn't clear from the question if this was the desired output, but the primary problem appears to be the use ofInteger
as opposed toDouble
.