2016-12-06 120 views
1

我有一個從參加名單中抽取名稱的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

看看這個。我已經將您的範圍聲明更改爲僅查看使用的範圍而不是較大的範圍(應加快您的代碼) 而不是循環遍歷範圍內的所有單元格,循環遍歷使用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 
相關問題