2011-05-19 52 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和更新),这非常耗时。