2014-09-24 89 views
0

我需要創建一個宏,它僅查看特定列(即不是整個電子表格)中的每個單元格,並從特定行開始。然後,它將包含所有不包含我的興趣值的行。例如我可以說我的目標是搜索列「A」中的每個值,然後我將從A2過濾到A99999999,使A1保持原樣。然後,我將刪除A列中不包含103526和103527的每一行根據列中的值刪除範圍內的行

下面的代碼能夠通過所有行篩選我感興趣的值,但是,我無法過濾僅一列,並且從A2到A99999999。我如何更改此代碼以符合這些條件?

Sub test()  
Dim j As Integer, k As Integer  
Dim r As Range, cfind6 As Range, cfind7 As Range  
Worksheets("sheet1").Activate  
On Error Resume Next  
j = Cells(Rows.Count, "A").End(xlUp).Row  
For k = j To 1 Step -1  
Set cfind6 = Rows(k).Cells.Find(what:=103526, lookat:=xlWhole)  

Set cfind7 = Rows(k).Cells.Find(what:=103527, lookat:=xlWhole)  
If cfind6 Is Nothing And cfind7 Is Nothing Then Rows(k).Delete  

Next 

回答

0

有幾種不同的方式來處理這一點,一個辦法是你的代碼更改爲類似:

Sub test() 
    Dim i As Integer 
    Dim ws As Worksheet 

    Set ws = ActiveWorkbook.Sheets("Sheet1") 

    For i = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row To 2 Step -1 
     If Not (ws.Cells(i, 1).Value = 103526 Or ws.Cells(i, 1).Value = 103527) Then 
      ws.Cells(i, 1).EntireRow.Delete 
     End If 
    Next i 
End Sub