Excel 2010 执行 Format(DateAdd("d", i, ), "[$-409]d-mmm;@") 时忽略年份
我正在使用 Excel 2010 VBA,并且正在创建一个基本时间线。下面是基于开始日期并跳过周末以“D-MMM”形式创建一些列标题的代码片段。
i = 0
' Add column headers based on the day of the week. Skip working on Saturday and Sunday; do not show weekends
Do
' D-MMM format = "[$-409]d-mmm;@"
ActiveCell.Value = Format(DateAdd("d", i, dteLowestStartDate), "[$-409]d-mmm;@")
ActiveCell.Interior.Color = RGB(153, 204, 255)
' IF it's a weekend do not print those days in the column
If Format(ActiveCell.Value, "dddd") = "Friday" Then
' Skip Saturday, Sunday
i = i + 3
Else
' Weekday
i = i + 1
End If
ActiveCell.Offset(0, 1).Select
'End processing more column headers once the start date + index(i) is a few days greater then end date
Loop Until DateAdd("d", i, dteLowestStartDate) > DateAdd("d", 3, dteHighestStartDate)
该代码执行并正确显示列标题。我遇到的问题是,当 1 月 1 日到来时,它会保留当前年份(今天是 2011 年),但我预计它是 2012 年。
作为测试,我删除了 FORMAT(... ", "[ $-409]d-mmm;@")
部分,因此它只是读取 DateAdd("d", i, dteLowestStartDate)
重新运行,并且它正确地增加了年份1 月 1 日,但随后我使用的使用 Application.Match
函数的代码失败了。
任何想法如何使列标题(顶行中的文本,跨越多个列)正确增加年份。 DateAdd
并使用 dd-mmm 格式?或者我应该考虑更改我的 Application.Match()
以便它不会返回错误 2042
这是 Match 的位置 ?失败:
colStartDate = Application.Match(CLng(dteStartDate), Range(colLetter & "1:XFD1"), 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来如果我只是将
FORMAT()
函数更改为Format(DateAdd("d", i, dteLowestStartDate), "Short Date")
就可以了。Looks like if I just change my
FORMAT()
function toFormat(DateAdd("d", i, dteLowestStartDate), "Short Date")
it works ok.