2011-05-19 78 views
0
Sub delRows() 
Dim cl  As Range 
Dim uRng As Range 
Dim rng As Range 
Dim x  As Long 

Set uRng = ActiveSheet.UsedRange 
For Each cl In uRng 
    If cl.Value = "0" Or cl.Value = "1" Or cl.Value = "2" Or cl.Value = "3" Then 
     If x = 0 Then 
      Set rng = cl 
      x = 1 
     Else: Set rng = Union(rng, cl) 
     End If 
    End If 
Next cl 
rng.EntireRow.Delete 
End Sub 

這是我寫的當前宏...它在整個頁面中搜索值; 0,1,2或3,然後刪除該行。沒關係。現在這個功能它工作得很好......我有問題改變它只是掃描單個列的這些值,然後刪除該行..我不斷收到錯誤。假設我想讓它只在工作表中的列「J」中搜索?有誰知道如何解決這個問題?Microsoft Excel 2010:幫助宏製作工作

回答

0
Sub delRows2() 
    Application.ScreenUpdating = False 
    Dim r As Long, r1 As Long, r2 As Long, c As Long 

    c = 10 '<--- this is the column you want to search 
    r1 = ActiveSheet.UsedRange.Row 
    r2 = r1 + ActiveSheet.UsedRange.Rows.Count - 1 

    For r = r2 To r1 Step -1 
     If Cells(r, c) = "0" or Cells(r, c) = "1" or Cells(r, c) = "2" or Cells(r, c) = "3" Then Cells(r, 1).EntireRow.Delete 
    Next r 
End Sub 
+0

工作得很好,非常感謝! – Leaum 2011-05-19 19:44:40

1

這只是需要改變你的一行代碼:

Set uRng = ActiveSheet.Range("J:J") 

爲了加快速度,你可以指定一個較小的範圍內,例如Range("J1:J100")或無論您的數據在哪裏。否則,您將遍歷整個列,即65536行(Excel 2003)或一百萬行(2007和更新),這非常耗時。