跳到主要内容

如何在Excel中列出两个日期之间的所有日期?

在某些情况下,如果您具有特定的开始日期和结束日期,则可能需要在Excel中列出这两个给定日期之间的所有日期。 现在,本教程将讨论在Excel中列出两个日期之间的所有日期的方法。

通过公式列出两个日期之间的所有日期

按VBA列出两个日期之间的所有日期

通过 Kutools for Excel 列出两个日期之间的所有日期 好主意3


在这里,我介绍了可以在Excel中快速列出两个给定日期之间的所有日期的公式。

1.在两个单元格中键入开始日期和结束日期,在这里我将它们键入到单元格A1和A2中。 看截图:
doc-list-all-dates-two-dates-1之间的文档列表

2.然后转到单元格C1键入此公式 = A1 + 1 进入它,然后单击 输入 键。 看截图:
doc-list-all-dates-two-dates-2之间的文档列表

3.然后在单元格C2中,键入此公式 =IF($A$1+ROW(A1)>=$A$2-1,"",C1+1) 放入其中,然后将自动填充手柄向下拖动到单元格,直到出现空白单元格。 查看屏幕截图:

doc-list-all-dates-two-dates-3之间的文档列表       doc-list-all-dates-two-dates-4之间的文档列表

然后,您可以看到列中列出了两个给定日期之间的所有日期。
doc-list-all-dates-two-dates-5之间的文档列表

请注意:

在上述公式中,A1是开始日期,A2是结束日期,C1是日期范围中的第一个日期。


如果您对宏代码感兴趣,则可以使用下面的VBA在Excel中列出两个给定日期之间的所有日期。

1.在两个单元格中输入您的开始日期和结束日期,在这里我输入单元格A1和B1。 看截图:
doc-list-all-dates-two-dates-6之间的文档列表

2。 按 Alt + F11 打开钥匙 Microsoft Visual Basic应用程序 窗口。

3。 然后点击 插页 > 模块 并将以下VBA代码复制并粘贴到弹出窗口中 模块 窗口。

VBA:列出两个日期之间的所有日期。

Sub WriteDates()
	'Updateby20150305
	Dim rng As Range
	Dim StartRng As Range
	Dim EndRng As Range
	Dim OutRng As Range
	Dim StartValue As Variant
	Dim EndValue As Variant
	xTitleId     = "KutoolsforExcel"
	Set StartRng = Application.Selection
	Set StartRng = Application.InputBox("Start Range (single cell):", xTitleId, StartRng.Address, Type: = 8)
	Set EndRng   = Application.InputBox("End Range (single cell):", xTitleId, Type: = 8)
	Set OutRng   = Application.InputBox("Out put to (single cell):", xTitleId, Type: = 8)
	Set OutRng   = OutRng.Range("A1")
	StartValue   = StartRng.Range("A1").Value
	EndValue     = EndRng.Range("A1").Value
	If EndValue - StartValue <= 0 Then
		Exit Sub
		End If
		ColIndex = 0
		For i = StartValue To EndValue
			OutRng.Offset(ColIndex, 0) = i
			ColIndex = ColIndex + 1
		Next
	End Sub

4。 点击 运行 or F5 运行VBA,并弹出一个对话框供您选择开始日期,然后单击 OK,然后在第二个弹出对话框中选择结束日期。 看截图:

doc-list-all-dates-two-dates-7之间的文档列表          doc-list-all-dates-two-dates-8之间的文档列表

5。 点击 OK,然后选择一个单元格以显示日期,然后单击 OK。 现在,您可以看到列出了两个日期之间的所有日期。 查看屏幕截图:

doc-list-all-dates-two-dates-9之间的文档列表         doc-list-all-dates-two-dates-10之间的文档列表

备注:此VBA生成的列表包括开始日期和结束日期。


实际上,如果您安装了 Kutools for Excel –方便的添加工具,您也可以使用 插入随机数据 解决这个问题。

Kutools for Excel, 与超过 300 方便的Excel功能,可提高工作效率并节省工作时间。

免费安装 Kutools for Excel,请执行以下操作:

1.选择要在两个日期之间列出日期的列,然后单击 库工具 > 插页 > 插入随机数据。 看截图:
doc插入随机数据1

2.然后在 插入随机数据 对话框中,单击 日期 标签,然后从中选择开始和结束日期 To列出,然后记得检查 Workday, 周末独特价值 复选框。 看截图:
doc-list-all-dates-two-dates-12之间的文档列表

3。 点击 Ok 关闭对话框,另一个 Kutools for Excel 对话框弹出,只需单击 USB MIDI(XNUMX通道)。 然后,您可以看到列出了开始日期和结束日期之间的日期。 查看屏幕截图:

doc-list-all-dates-two-dates-13之间的文档列表           doc-list-all-dates-two-dates-14之间的文档列表

4.现在,您需要按所需顺序对日期列表进行排序。 请点击 时间 > 从最旧到最新排序。 然后,您可以看到日期从最早的日期到最新的日期排序。 查看屏幕截图:

doc-list-all-dates-two-dates-15之间的文档列表          doc-list-all-dates-two-dates-16之间的文档列表

插入随机数据 实用程序中,您还可以插入随机整数,随机字符串和随机时间等。 单击此处以了解有关插入随机数据的更多信息。


相关文章:


最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
How to loop this vba code (1000 rows for example) ?
This comment was minimized by the moderator on the site
Sorry for reply such late, but I do not understand your question, the VBA only for listing dates between a date range, why need to loop the code?
This comment was minimized by the moderator on the site
Firat - did you solve your issue? I have exactly the same issue and I cannot get the result in the row instead of the column.
This comment was minimized by the moderator on the site
If you invert this line you can make it work :


OutRng.Offset(ColIndex, 0) = i to OutRng.Offset(0, ColIndex) = i
This comment was minimized by the moderator on the site
Why do not try to transpose the column result to row?
This comment was minimized by the moderator on the site
i tried the VBA code it worked.. Thanks for sharing. Similarly is it possible to pase it along columns/ horizontally?
This comment was minimized by the moderator on the site
Hello, if you want to list dates in a row horizontally, you just need to use the vba code to list the dates, and copy the results and paste transpose.
This comment was minimized by the moderator on the site
Hello, Thanks for sharing a great code. I would like to ask one question though. I am using this VBA code you shared. 1) Can I list all the other cells in the same row with the dates? 2) Can we define the starting date cell and ending date cell and the cell that the new information will be written? I am asking these questions because I have 30 rows. Each row has data for different people. Cell G is a starting date and Cell H is an ending date. Other cells contains some information. I would like this to be listed in a new cell as all the dates between these cells. For example (just showing demonstration, so only G and H cells written below-I is where the list appears): Row 2 Person A 28/05/2017 05/06/2017 28/05/2017 Row 3 Person A 28/05/2017 05/06/2017 29/05/2017 Row 4 Person A 28/05/2017 05/06/2017 30/05/2017 Row 5 Person A 28/05/2017 05/06/2017 31/05/2017 Row 6 Person A 28/05/2017 05/06/2017 01/06/2017 Row 7 Person A 28/05/2017 05/06/2017 02/06/2017 Row 8 Person A 28/05/2017 05/06/2017 03/06/2017 Row 9 Person A 28/05/2017 05/06/2017 04/06/2017 Row 10 Person A 28/05/2017 05/06/2017 05/06/2017 Row 11 Person B 23/05/2017 31/05/2017 23/05/2017 Row 12 Person B 23/05/2017 31/05/2017 24/05/2017 Row 13 Person B 23/05/2017 31/05/2017 25/05/2017 Row 14 Person B 23/05/2017 31/05/2017 26/05/2017 and so on...
This comment was minimized by the moderator on the site
Can we use text box instead of in box in macro
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations