gpt4 book ai didi

vba - 获取一个月的最后一天

转载 作者:行者123 更新时间:2023-12-03 09:45:27 33 4
gpt4 key购买 nike

我想得到这个月的最后一天。
这是我的代码。如果我想调试它并将其编译到数据库,它会说它有语法错误。

Public Function GetNowLast() As Date
Dim asdfh As Date
asdfh = DateValue("1." _
& IIf(Month(Date) + 1) > 12, Month(Date) + 1 - 12, Month(Date) + 1) _
&"."&IIf(Month(Date)+1)>12 , Year(Date)+1,Year(Date))
asdf = DateAdd("d", -1, asdf)
GetNowLast = asdf
End Function

最佳答案

GD Linux人,

让我们专注于获得结果...:-)

另见:here

@Scott Craner 的评论是正确的!虽然严格来说没有必要使用格式。 (假设您想使用“日期”对象)

要实现您想要的功能,请按以下方式设置功能:

Function GetNowLast() as Date

dYear = Year(Now)
dMonth = Month(Now)

getDate = DateSerial(dYear, dMonth + 1, 0)

GetNowLast = getDate

End Function

您可以在代码中调用该函数:
Sub findLastDayOfMonth()

lastDay = GetNowLast()

End Sub

或者,更整洁的可能是:
Function GetNowLast(inputDate as Date) as Date

dYear = Year(inputDate)
dMonth = Month(inputDate)

getDate = DateSerial(dYear, dMonth + 1, 0)

GetNowLast = getDate

End Function

您可以调用该函数并将输入参数传递给它。
Sub findLastDayOfMonth()

lastDay = GetNowLast(Now()) 'Or any other date you would like to know the last day of the month of.

End Sub

另见 this @KekuSemau 的巧妙解决方案

关于vba - 获取一个月的最后一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33111285/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com