excel - 如果单元格包含今天的日期,则删除整行
问题描述
我正在尝试在 Excel 中编写一个宏来执行多项任务,如果当前日期在 A 列中,则最终任务是删除整行。
这是我记录的。我尝试使用我在网上找到的其他脚本来完成最终任务,但它没有完成。我没有收到任何类型的错误消息。
Sub NCR()
'
' NCR Macro
'
'
Cells.Select
With Selection
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:B").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Columns("H:K").Select
Selection.Delete Shift:=xlToLeft
Rows("2:1048576").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"E2:E1076"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:AI1076")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$AI$84").AutoFilter Field:=5, Criteria1:= _
"In Progress"
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Cells.Select
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Cells.EntireColumn.AutoFit
Rows("2:1048576").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"A2:A1048501"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:AI1048501")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("B14").Select
With Range("A2:A500" & lastrow)
.AutoFilter 1, "=" & CLng(Date)
.Offset(1).EntireRow.Delete
.AutoFilter
End With
End Sub
我绝不是录制宏的专家,但在我进入“Range B14 Select”之前一切正常。之后,脚本停止,没有任何错误消息。
我究竟做错了什么?如何解决这个问题?
解决方案
代替
With Range("A2:A500" & lastrow)
.AutoFilter 1, "=" & CLng(Date)
.Offset(1).EntireRow.Delete
.AutoFilter
End With
和
Dim i
For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
If Cells(i, 1) = Date Then Rows(i).Delete
Next
推荐阅读
- java - 如何获取有关某些 StorageReference 的子项的信息?
- datetime - 获取异常输入字符串的格式不正确
- c# - 比较多个对象列表中的字符串
- python - 为什么我在 X-Cube-AI / STM32CubeMX.AI 中的压缩级别“无”和“4x”得到完全相同的权重?
- c# - Visual Studio 2019 未显示 .NET 5 框架
- batch-file - 将文本放入无法从 mediainfo 工作的变量中
- javascript - 如何在 CSS 或 Javascript 中的所有页面上添加通知?
- django - 更新后 Manjaro linux 中 Postgrestql 和 Django 的问题
- node.js - Node.js 模块如何影响服务器带宽?
- angular - 角子组件