), "[$-409]d-mmm;@") 时忽略年份" />

Excel 2010 执行 Format(DateAdd("d", i, ), "[$-409]d-mmm;@") 时忽略年份

发布于 2024-12-19 13:10:39 字数 1408 浏览 3 评论 0 原文

我正在使用 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)

I'm using Excel 2010 VBA and I'm creating a basic timeline. Here is the snippet of code that creates some column headers, in the form of "D-MMM", based off of a start date and skipping weekends.

   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)

The code executes and displays the column headers correctly. The issue I have is that when January 1 comes around, it keeps the current year (today that is 2011) but I expected it to be 2012.

As a test, I remove the FORMAT(... ", "[$-409]d-mmm;@") part, so it just reads DateAdd("d", i, dteLowestStartDate) rerun, and it correctly increments the year on Jan 1 but then subsequent code I use that makes use of the Application.Match function fails.

Any idea how to make the column headers (text in top row, spanning multiple columns) correctly increment the year with DateAdd and with the dd-mmm format? OR should I be looking at changing my Application.Match() so it doesn't return error 2042?

Here is where the Match fails:

colStartDate = Application.Match(CLng(dteStartDate), Range(colLetter & "1:XFD1"), 0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-12-26 13:10:39

看起来如果我只是将 FORMAT() 函数更改为 Format(DateAdd("d", i, dteLowestStartDate), "Short Date") 就可以了。

Looks like if I just change my FORMAT() function to Format(DateAdd("d", i, dteLowestStartDate), "Short Date") it works ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文