获得年度的第一天和最后一天

Icy*_*rts 4 excel vba date excel-vba excel-2013

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

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

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

testdate = "03/21/2017"
Run Code Online (Sandbox Code Playgroud)

并获得以下值

firstdate = "01/01/2017"
lastdate = "12/31/2017"
Run Code Online (Sandbox Code Playgroud)

Fad*_*adi 9

你可以使用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
Run Code Online (Sandbox Code Playgroud)