【问题标题】:VBA to write each day between a data range in ExcelVBA每天在Excel中的数据范围之间写入
【发布时间】:2017-01-23 13:22:26
【问题描述】:

我需要在 Excel 中制作一个表格,询问开始和结束日期。然后,我需要编写一个 VBA 脚本,在 A 列的第一个空白单元格中写出该范围内的每一天。

因此,例如,如果给出:

Start Date: 1/5/2017
End Date: 1/9/2017

结果是:

1/5/2017
1/6/2017
1/7/2017
1/8/2017
1/9/2017

然后,如果它以新的日期范围再次运行,日期将附加到列表的底部。这只是一个简短的示例,实际上日期范围会更大,并且包含几个月。

我不确定从哪里开始,所以任何帮助将不胜感激!

【问题讨论】:

  • Excel 将日期存储为带有1 = 1 Jan 1900 的数字。所以编码应该很简单。请注意,这不是免费的代码编写服务,它的存在是为了帮助其他人编写他们正在尝试开发的代码或功能。因此,我们希望看到清晰的问题,包括数据示例、尝试的代码、实际输出、期望的输出、解决问题的研究工作等。请阅读帮助页面以获取有关 How to Ask a Good QuestionHow to create a Minimal, Complete, and Verifiable example 的信息

标签: vba excel


【解决方案1】:

正如@Ron Rosenfeld 提到的,VBA 中的日期只是一个可以通过简单的数字运算增加或减少的数字。这段代码应该完全符合您的要求:

Dim startDate As Date
Dim endDate As Date

startDate = DateSerial(2017, 1, 1)
endDate = DateSerial(2017, 1, 23)

Dim sheet As Worksheet
Set sheet = Worksheets("Table1")
Dim i As Integer
i = 1

While startDate <= endDate
    sheet.Cells(i, 1) = startDate
    startDate = startDate + 1
    i = i + 1
Wend

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多