我在 Excel 2016 中创建了这个宏,但问题在 Excel 2019 中仍然存在。
我的电子表格有一个宏,用于删除特定列中单元格包含单词“已关闭”的所有行。该宏大部分情况下有效,但仍有 2-3 行保留。再次运行该宏将清除它们。
我通过标题中的文本查找列号是否会让事情变得更困难?我可能可以对其进行硬编码;我只是想让它具有前瞻性,以防我以后想移动该列。
宏:
Sub RemoveClosed()
' Select table
Dim rObj As ListObject
Set rObj = ActiveSheet.ListObjects("myTable")
' Clear any applied filters
On Error Resume Next 'If filters are already cleared, don't throw a wobbly
ActiveSheet.ShowAllData
' Recalculate all formulas
Application.Calculate
' Get the column number of the 'Closure' column
Dim closureCol As Long
closureCol = rObj.HeaderRowRange.Find("Closure").Column
' Specify what we're going to look for and use as deletion criteria
Dim lookupValue As String
lookupValue = "Closed"
' Start a counter for the If loop
Dim zed As Integer
' If loop:
For zed = 1 To rObj.ListRows.Count ' Counter cycles from 1 to total number of data rows
If Cells(zed, closureCol).Value = lookupValue Then ' If cell in Closure column in row 'zed' contains the deletion criterion
If Not Application.CalculationState = xlDone Then
DoEvents
End If
Rows(zed).EntireRow.Delete ' Delete entire row
End If
Next zed
End Sub