gpt4 book ai didi

vba - 获取一年中的第一天和最后一天

转载 作者:行者123 更新时间:2023-12-02 09:17:54 28 4
gpt4 key购买 nike

在 VBA 中,我知道您可以使用此语法从日期中减去年份

Dim testdate As String, DateTest As String
testdate= "03/21/2017"
DateTest = Month(testdate) & "/" & Day(testdate) & "/" & Year(testdate) - 1

但是如何找到给定年份的第一个和最后一个日期呢?例如,让我们使用相同的日期

testdate = "03/21/2017"

并获取以下值

firstdate = "01/01/2017"
lastdate = "12/31/2017"

最佳答案

您可以使用DateSerial:

Sub Test()

Dim dt As Date, firstDay As Date, lastDay As Date

dt = Date
firstDay = DateSerial(Year(dt), 1, 1)
lastDay = DateSerial(Year(dt), 12, 31)

Debug.Print firstDay
Debug.Print lastDay

End Sub

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

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