跳至主要内容

如何在Excel中根据日期将行复制并粘贴到另一个工作表?

Author: Xiaoyang Last Modified: 2025-07-31

假设我有一个数据区域,现在,我想根据特定日期复制整行,然后将它们粘贴到另一个工作表中。您是否有好的方法来在Excel中处理这个任务?

根据今天的日期复制行并粘贴到另一个工作表

如果日期大于今天,则复制行并粘贴到另一个工作表


根据今天的日期复制行并粘贴到另一个工作表

如果您需要在日期为今天时复制行,请应用以下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


最佳 Office 办公效率工具

🤖 Kutools AI 助手:基于智能执行,彻底革新数据分析 生成代码创建自定义公式分析数据并生成图表调用 Kutools Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!