我试图在2018年全年都这样做,当我运行我的代码时,我得到了2017年。这是我的代码
Sub MakeSheetForEachDay()
'Will copy the sheet named "START" and rename as
'as a date, 1-Jan, 2-Jan, etc.
'
Dim wsStart As Worksheet
Dim strDate As String
Dim mth As Long, dy As Long
Set wsStart = Sheets("START")
For mth = 1 To 12
For dy = 1 To Day(DateSerial(Year(2018), mth + 1, 1) - 1)
strDate = Format(DateSerial(Year(2018), mth, dy), "ddd mmm d")
wsStart.Copy after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = strDate
Next dy
Next mth
End Sub
答案 0 :(得分:2)
简短回答不要使用Year(2018)
只使用DateSerial(2018, mth + 1, 1)
如果您查看documentation for year,您会将其定义为
Public Function Year(ByVal DateValue As DateTime) As Integer
这意味着在评估Year(2018)
时,它必须将整数2018转换为DateTime。它通过将2018天添加到7/10/1905的时代来实现这一点。所以Year(2018)
返回1905年。
恰好,Format(DateSerial(2017, mth, dy), "ddd mmm d")
和Format(DateSerial(1905, mth, dy), "ddd mmm d")
返回相同的值,因为Jan,1 1905和2017都是星期日。这就是为什么你对Format(DateSerial(Year(2018), mth, dy), "ddd mmm d")