VBA:循环variables的string数组,如果匹配值,则删除行

我有多个文本variables(水果),当他们出现在列RI上需要删除整个行。 我有下面的代码,但因为我有15个variables的“水果”(并可能需要添加更多的未来)我想知道如何使这是一个循环的单个子程序。 我尝试了很多失败的方式。 可能包含所有名称的string数组?

提前致谢,

Sub Fix1() Dim Fruit As String Fruit = "Apple" Do On Error GoTo ByeBye Range("R:R").Find(Fruit).EntireRow.Delete Loop ByeBye: Exit Sub End Sub Sub Fix2() Dim Fruit As String Fruit = "Orange" Do On Error GoTo ByeBye Range("R:R").Find(Fruit).EntireRow.Delete Loop ByeBye: Exit Sub End Sub 

你可以使用AutoFiter()

 Sub FixAll() Dim Fruits As Variant Fruits = Array("Apple", "Banana", "Pear") '<--| list your fruits With Range("R:R") '<--| reference your range .AutoFilter Field:=1, Criteria1:=Fruits, Operator:=xlFilterValues '<--| filter referenced column with 'Fruits'" content If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete '<--| if any cell found other than header one then delete its corresponding row End With ActiveSheet.AutoFilterMode = False End Sub 

如果你不需要删除第一行:

 Dim r As Range Set r = ActiveSheet.UsedRange r.AutoFilter 19 - r.Column, Array("Apple", "Orange"), xlFilterValues r.Offset(1).Delete xlShiftUp ' deletes the non-filtered rows r.AutoFilter ' hide the filter