【问题标题】:Populate a column with dates starting from 1 jan to 31 dec given the year在给定年份的情况下填充日期从 1 月 1 日到 12 月 31 日的列
【发布时间】:2019-11-10 23:24:35
【问题描述】:

我想在 excel-vba 中编写一个代码,将特定年份的日期填充到工作表的列中。
该代码还应考虑闰年。假设我在单元格 B1 中输入了年份(如 2020 年),因此 A 列应填充 366 个日期条目(考虑闰年)。

【问题讨论】:

  • Excel 具有开箱即用的功能(填充/系列),但如果您想重新编程,请随意。但是 - 你的问题是什么?
  • 基本上excel不考虑闰年,但在VBA的帮助下,我认为可以做到。我如果用 excel 填充 366 行,那么最后一行将是“#Value”或类似的东西,持续 4 年中的 3 年

标签: vba excel


【解决方案1】:

您可以循环使用 Date 变量:

Sub dater()
    Dim yr As Long, d As Date, i As Long
    yr = Range("B1").Value
    For d = DateSerial(yr, 1, 1) To DateSerial(yr, 12, 31)
        i = i + 1
        Cells(i, 1) = d
    Next d
End Sub

这将自动处理闰年并根据需要填充 366 或 365 单元格。

(格式化列 A 以满足您的需要)

【讨论】:

  • 谢谢,我从没想过你可以在 For 循环中设置一个变量。加
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2021-07-13
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多