如何遍历已过滤的范围并删除单元格

我有一个从参加名单中抽取名字的excel工作表。 我有过滤单元格的范围,以便用户可以select在某些经理下的名字。

我有一系列的专栏,可以扫描员工的徽章,并且与员工相关的信息将被填充。

最终的目的是为了当有人被扫描到表格中时,他们的名字将从出席名单中删除,这样就很容易看到谁被扫描,谁不被扫描。 我的代码工作,如果工作表没有被过滤,但每个经理下的每个员工的名字是没有帮助的,所以我希望能够有使用filter,只看到他们想看的人,而仍然能够删除名单中的名字。 谁能帮我吗?

Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range Set KeyCells = Range("B7:B100000") If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then Call LoopMatch End If End Sub Sub LoopMatch() Dim Scanned, NotScanned As Range Dim i, j As Range Set Scanned = Worksheets("Main").Range("C7:C100000") Set NotScanned = Worksheets("Main").Range("J7:J100000") For Each i In Scanned.Cells For Each j In NotScanned.Cells If i.Value = j.Value Then Range("J" & j.Row & ":L" & j.Row).Delete Exit Sub Else End If Next j Next i End Sub 

看看这个。 我已经改变你的范围声明只看看使用的范围,而不是一个大的(应该加快你的代码)而不是遍历范围内的所有单元格,通过使用SpecialCells(xlCellTypeVisible)循环,这将循环只通过过滤范围

 Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range With Me Set KeyCells = .Range(.Cells(7, 2), .Cells(.Cells(.Rows.Count, 2).End(xlUp).Row, 2)) End With If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then Call LoopMatch End If End Sub 

 Sub LoopMatch() Dim Scanned, NotScanned As Range Dim i, j As Range With Worksheets("Main") Set Scanned = .Range(.Cells(7, 3), .Cells(.Cells(.Rows.Count, 3).End(xlUp).Row, 3)) Set NotScanned = .Range(.Cells(7, 10), .Cells(.Cells(.Rows.Count, 10).End(xlUp).Row, 10)) End With For Each i In Scanned.SpecialCells(xlCellTypeVisible) For Each j In NotScanned.SpecialCells(xlCellTypeVisible) If i.value = j.value Then Range("J" & j.Row & ":L" & j.Row).Delete Exit Sub Else End If Next j Next i End Sub