簡體   English   中英

如何循環過濾范圍並刪除單元格

[英]How to loop through filtered range and delete cells

我有一個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

看看這個。 我已經將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM