Skip to main content

如何根據Excel中的日期將行複製並貼到另一個工作表?

Author: Xiaoyang Last Modified: 2025-05-12

假設我有一組數據,現在,我想根據特定日期複製整個行,然後將它們貼到另一個工作表中。您有什麼好辦法來處理這個任務嗎?

根據今天的日期複製行並將其貼到另一個工作表

如果日期大於今天,則複製行並將其貼到另一個工作表


根據今天的日期複製行並將其貼到另一個工作表

如果您需要在日期為今天的條件下複製行,請應用以下VBA代碼:

1. 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2. 點擊 插入 > 模塊,然後在模塊窗口中粘貼以下代碼。

VBA代碼:根據今天的日期複製和粘貼行:

Sub CopyRow() 'Updateby Extendoffice Dim xRgS As Range, xRgD As Range, xCell As Range Dim I As Long, xCol As Long, J As Long Dim xVal As Variant On Error Resume Next Set xRgS = Application.InputBox("Please select the date column:", "KuTools For Excel", Selection.Address, , , , , 8) If xRgS Is Nothing Then Exit Sub Set xRgD = Application.InputBox("Please select a destination cell:", "KuTools For Excel", , , , , , 8) If xRgD Is Nothing Then Exit Sub xCol = xRgS.Rows.Count Set xRgS = xRgS(1) Application.CutCopyMode = False J = 0 For I = 1 To xCol Set xCell = xRgS.Offset(I - 1, 0) xVal = xCell.Value If TypeName(xVal) = "Date" And (xVal <> "") And (xVal = Date) Then xCell.EntireRow.Copy xRgD.Offset(J, 0) J = J + 1 End If Next Application.CutCopyMode = True End Sub 

3. 粘貼上述代碼後,請按 F5 鍵運行此代碼,隨後會彈出一個提示框,提醒您選擇要基於其複製行的日期列,請參見截圖:

a screenshot of selecting the date range after running the code

4. 然後點擊 確定 按鈕,在另一個提示框中,選擇另一個工作表中您希望輸出結果的單元格,請參見截圖:

a screenshot of selecting a destination cell

5. 然後點擊 確定 按鈕,現在,日期為今天的行已經立即被粘貼到新工作表中,請參見截圖:

a screenshot of the results


如果日期大於今天,則複製行並將其貼到另一個工作表

要複製和粘貼日期大於或等於今天的行,例如,如果日期自今天起等於或大於5天,則將這些行複製並粘貼到另一個工作表中。

以下VBA代碼可能會對您有所幫助:

1. 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2. 點擊 插入 > 模塊,然後在模塊窗口中粘貼以下代碼。

VBA代碼:如果日期大於今天,則複製和粘貼行:

Sub CopyRow() 'Updateby Extentoffice Dim xRgS As Range, xRgD As Range, xCell As Range Dim I As Long, xCol As Long, J As Long Dim xVal As Variant On Error Resume Next Set xRgS = Application.InputBox("Please select the date column:", "KuTools For Excel", Selection.Address, , , , , 8) If xRgS Is Nothing Then Exit Sub Set xRgD = Application.InputBox("Please select a destination cell:", "KuTools For Excel", , , , , , 8) If xRgD Is Nothing Then Exit Sub xCol = xRgS.Rows.Count Set xRgS = xRgS(1) Application.CutCopyMode = False J = 0 For I = 1 To xCol Set xCell = xRgS.Offset(I - 1, 0) xVal = xCell.Value If TypeName(xVal) = "Date" And (xVal <> "") And (xVal >= Date And (xVal < Date + 5)) Then xCell.EntireRow.Copy xRgD.Offset(J, 0) J = J + 1 End If Next Application.CutCopyMode = True End Sub 

備註:在上述代碼中,您可以更改條件,例如小於今天或根據需要在 If TypeName(xVal) = "Date" And (xVal <> "") And (xVal >= Date And (xVal < Date + 5)) Then 腳本代碼中的天數。

3. 然後按 F5 鍵運行此代碼,在提示框中,請選擇您要使用的數據列,請參見截圖:

a screenshot of selecting the date range after running the code 2

4. 然後點擊 確定 按鈕,在另一個提示框中,選擇另一個工作表中您希望輸出結果的單元格,請參見截圖:

a screenshot of selecting a destination cell to place the rows if date is greater than today

5. 點擊 確定 按鈕,現在,日期自今天起等於或大於5天的行已經被複製並粘貼到新工作表中,如下截圖所示:

a screenshot showing the rows only when dates are greater than today


最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!