使用vba(autofilter)删除符合excel中某些条件的行

时间:2013-06-17 17:51:37

标签: vba autofilter

我正在尝试使用自动过滤器根据条件删除行...因为我正在使用的工作表中有很多数据。如下所示,我在此网站上找到了一个代码,我编辑该代码以符合我需要的标准。代码运行没有错误,但不删除任何行!任何帮助,将不胜感激!我希望找到标准的列是B列。

Private Sub CommandButton2_Click()
Dim rngTable As Range
Dim ws As Worksheet
Dim StartCell As Range

Const ColumntoFilter1 As Integer = 2
Const FilterCriteria1 As String = "Amended Holiday"
Const ColumntoFilter2 As Integer = 2
Const FilterCriteria2 As String = "Designated Holiday"
Const ColumntoFilter3 As Integer = 2
Const FilterCriteria3 As String = "Max Sick Hours"
Const ColumntoFilter4 As Integer = 2
Const FilterCriteria4 As String = "Leave Without Pay 02"
Const ColumntoFilter5 As Integer = 2
Const FilterCriteria5 As String = "Holiday Accrued"
Const ColumntoFilter6 As Integer = 2
Const FilterCriteria6 As String = "Leave Without Pay 03"
Const ColumntoFilter7 As Integer = 2
Const FilterCriteria7 As String = "Max Holiday Hours"
Const ColumntoFilter8 As Integer = 2
Const FilterCriteria8 As String = "Leave Without Pay 04"
Const ColumntoFilter9 As Integer = 2
Const FilterCriteria9 As String = "Sick Accrued"

Set ws = ActiveSheet
'Set the starting position (Top-left most position) of your data range
Set StartCell = ws.Range("B1")

'Turn off autofilter in case it's already active
ws.AutoFilterMode = False
   'Define data table
    Set rngTable = StartCell.CurrentRegion
    'Filter and delete data
    With rngTable
    .AutoFilter Field:=ColumntoFilter1, Criteria1:=FilterCriteria1
    .AutoFilter Field:=ColumntoFilter2, Criteria1:=FilterCriteria2
    .AutoFilter Field:=ColumntoFilter3, Criteria1:=FilterCriteria3
    .AutoFilter Field:=ColumntoFilter4, Criteria1:=FilterCriteria4
    .AutoFilter Field:=ColumntoFilter5, Criteria1:=FilterCriteria5
    .AutoFilter Field:=ColumntoFilter6, Criteria1:=FilterCriteria6
    .AutoFilter Field:=ColumntoFilter7, Criteria1:=FilterCriteria7
    .AutoFilter Field:=ColumntoFilter8, Criteria1:=FilterCriteria8
    .AutoFilter Field:=ColumntoFilter9, Criteria1:=FilterCriteria9
    .Offset(1, 0).EntireRow.Delete
End With

'Turn filters off again
ws.AutoFilterMode = False

Set rngTable = Nothing
Set StartCell = Nothing
Set ws = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

每个AutoFilter语句都会覆盖前一个语句,因此最后,您的代码只删除B列中Sick Accrued的所有行 - 尝试在每个自动过滤器之间添加delete语句:

With rngTable
.AutoFilter Field:=ColumntoFilter1, Criteria1:=FilterCriteria1
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter2, Criteria1:=FilterCriteria2
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter3, Criteria1:=FilterCriteria3
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter4, Criteria1:=FilterCriteria4
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter5, Criteria1:=FilterCriteria5
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter6, Criteria1:=FilterCriteria6
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter7, Criteria1:=FilterCriteria7
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter8, Criteria1:=FilterCriteria8
.Offset(1, 0).EntireRow.Delete
.AutoFilter Field:=ColumntoFilter9, Criteria1:=FilterCriteria9
.Offset(1, 0).EntireRow.Delete