我有一张excel工作表,可以从出勤名单中提取姓名。我有过滤的单元格范围,以便用户可以在某些经理下选择名称。
我有一系列专栏可以扫描员工的徽章,并填写与员工相关的信息。
最终目标是让有人在扫描表格时,他们的姓名将从出勤名单中删除,以便很容易看到谁被扫描,谁没有。我的代码可以工作,如果没有过滤工作表,但每个经理下的每个员工的名字都没有帮助,所以我希望能够使用过滤器只看到他们想要看到的人,同时仍然可以删除名单上的名字。任何人都可以帮助我吗?
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
答案 0 :(得分:0)
看看这个。我已经将Range声明更改为仅查看已使用的范围而不是大范围(应该加快代码速度)
不是循环遍历范围内的所有单元格,而是使用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