跳到主要內容

如何在Excel中列出兩個日期之間的所有日期?

在某些情況下,如果您具有特定的開始日期和結束日期,則可能需要在Excel中列出這兩個給定日期之間的所有日期。 現在,本教程將討論在Excel中列出兩個日期之間的所有日期的方法。

通過公式列出兩個日期之間的所有日期

按VBA列出兩個日期之間的所有日期

通過Kutools for Excel列出兩個日期之間的所有日期 好主意3


在這裡,我介紹了可以在Excel中快速列出兩個給定日期之間的所有日期的公式。

1.在兩個單元格中鍵入開始日期和結束日期,在這裡我將它們鍵入到單元格A1和A2中。 看截圖:
doc-list-all-dates-two-dates-1之間的文檔列表

2.然後轉到單元格C1鍵入此公式 = A1 + 1 進入它,然後單擊 Enter 鍵。 看截圖:
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 for Applications 窗口。

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生成的列表包括開始日期和結束日期。


實際上,如果您安裝了 Excel的Kutools –方便的添加工具,您也可以使用 插入隨機數據 解決這個問題。

Excel的Kutools, 與以上 300 便捷的Excel功能,可提高工作效率並節省工作時間。

免費安裝 Kutools for Excel,請執行以下操作:

1.選擇要在兩個日期之間列出日期的列,然後單擊 庫工具 > 插入 > 插入隨機數據。 看截圖:
doc插入隨機數據1

2.然後在 插入隨機數據 對話框中單擊 日期 標籤,然後從中選擇開始和結束日期 To列出,然後記得檢查 勞動日, 週末獨特價值 複選框。 看截圖:
doc-list-all-dates-two-dates-12之間的文檔列表

3。 點擊 Ok 關閉對話框,另一個 Excel的Kutools 對話框彈出,只需單擊 。 然後,您可以看到列出了開始日期和結束日期之間的日期。 查看屏幕截圖:

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之間的文檔列表

這款獨特的敏感免洗唇膜採用 Moisture WrapTM 技術和 Berry Mix ComplexTM 成分, 插入隨機數據 實用程序中,您還可以插入隨機整數,隨機字符串和隨機時間等。 單擊此處以了解有關插入隨機數據的更多信息。


相關文章:


最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為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