excel - 删除最后几个工作日
问题描述
我正在尝试删除所有包含过去两天(今天和昨天)日期的行。
H 列是日期列。
这是代码的相关位:
Dim lstRow As Long, Idx As Long
lstRow = Range("H" & Cells.Rows.Count).End(xlUp).row
For Idx = lstRow To 2 Step -1 ' change 1 to the first row with dates
If Cells(Idx, "H") > Application.WorksheetFunction.WorkDay(Date, -1) Then
Cells(Idx, "H").EntireRow.Delete
End If
Next
它没有任何效果。完整的日期范围(过去 5 天)仍包含在数据中。
如果我将 > 反转为 <,则只保留新日期,这正是我想要摆脱的。
解决方案
针对日期样本(从 01/07/2018 到 10/07/2018 [今天])运行此代码后,此代码删除了显示今天日期的行。如果您还想删除前一个工作日,则需要将“-1”更改为“-2”,如下所示:
Dim lstRow As Long, Idx As Long
lstRow = Range("H" & Cells.Rows.Count).End(xlUp).row
For Idx = lstRow To 2 Step -1 ' change 1 to the first row with dates
If Cells(Idx, "H") > Application.WorksheetFunction.WorkDay(Date, -2) Then
Cells(Idx, "H").EntireRow.Delete
End If
Next
此外,您没有说明它,但是当您使用“Application.WorksheetFunction.WorkDay”时,它还将删除该窗口中非工作日的任何日期(因此在我的示例数据中,第 7 和第 8 也被删除但第一个不是)。
推荐阅读
- azure - 将过滤后的消息保存在 Azure 服务总线主题订阅中
- php - 如何在 MySql 的 case 中编写子查询?
- c# - 如何绑定到 DataGridTemplateColumn 内的 Combobox
- asp.net-core - 我已经为身份用户创建了一个角色,但是当我使用它时说未授权
- swift - 设置 UINavigationController 标题中单个单词的字体
- android - 从 API 检索数据并使用颤振显示
- amazon-athena - Athena MSCK 修复表返回“不在 Metastore 中的表”
- python - 尝试使用工具,但出现以下错误 KeyError: 'str'
- php - 使用 composer 安装包
- android - 我可以在 onCharacteristicRead() 方法而不是 onCharacteristicChanged() 中获得特征响应吗