我目前正在尝试使用.Find来搜索以“K”开头的项目数组。如果匹配,则继续过滤并删除该项目。但是,我不确定.Find是否能够将数组合并到其条件中。我考虑过使用For each和If,但代码会相当长。任何人都可以帮助或提出不同方法的建议吗?
Dim ckFOH As Range
Dim Krange As Variant
Krange = Sheets("Master List").Range("G17:G" & Range("G17").End(xlDown).Row)
With Sheets("FOH")
Set ckFOH = .Columns("Q").Find(What:=Krange, LookIn:=xlValues)
If Not ckFOH Is Nothing Then
.Rows("5").AutoFilter Field:=17, Criteria1:="=K*"
.Range("A6:K" & Range("A6").End(xlDown).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
End With
答案 0 :(得分:1)
Find()
对象的 Range
方法接受其"什么"的任何数据类型。参数,但如果您提供Range
(根据您的代码)或甚至是一维数组,它只是实际搜索的第一个元素
此外,根据您的描述,我相信您要删除所有表格" FOH"具有任何实际" K"的行在"主列表"中找到的值第Q栏
因此您可能希望使用AutoFilter()
并直接过滤所有提供数组Criteria1
参数的值并激活其xlFilterValues
运算符选项的Q列
按照以下代码(评论中的进一步说明):
Option Explicit
Sub main()
Dim Krange As Variant
With Sheets("Master List") 'reference wanted sheet
Krange = Application.Transpose(.Range("G17", .Range("G17").End(xlDown)).Value) ' store referenced sheet column G values from row 17 down to last consecutive not empty cell - explicitly qualify ALL range references to referenced worksheet
End With
With Sheets("FOH") 'reference wanted sheet
With .Range("Q5", .Cells(.Rows.Count, "Q").End(xlUp)) 'reference its column Q range from row 5 (header) to last not empty row
.AutoFilter field:=1, Criteria1:=Krange, Operator:=xlFilterValues ' filtere referenec range on all 'Krange' array values
If CBool(Application.Subtotal(103, .Cells)) > 1 Then .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete ' if any filtered cells other then header, thene delete their entire rows
End With
.AutoFilterMode = False
End With
End Sub