Microsoft Excel 2010:帮助使宏工作

时间:2011-05-19 19:13:33

标签: excel vba

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然后删除该行。没关系。现在对于这个功能它完全正常...我有问题将它改为JUST扫描单个列的那些值然后删除行..我不断收到错误。就像让我们说我希望它只是在工作表中的“J”列中搜索一样?有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

这只需要更改代码中的一行:

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

为了加快速度,您可以指定较小的范围,例如Range("J1:J100")或您的数据所在的位置。否则,您将遍历整个列,即65536行(Excel 2003)或一百万行(2007及更新版本),这非常耗时。

答案 1 :(得分: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