本文介绍了根据标准删除Excel中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道正确的方法,但我都被搜索出来。我正在搜索一个包含各种垃圾的列(资产标签),但希望保留以AA开头的整行,因为这确认行实际上是一个资产标签。



下面的代码给我一个对象必需错误,我相信我可能没有正确地告诉它查看单元格值与如果rng.Cells(i)<>左(cell.Value,2) =AA然后声明。有人可以指出我需要做的正确方向吗?

  Sub DeleteRows()

Dim rng As Range
Dim i As Double,counter As Double
Set rng = Range(C:C)
i = 1
对于counter = 1 to rng.Rows .count
如果rng.Cells(i)<>左(cell.Value,2)=AA然后
rng.Cells(i).EntireRow.Delete
Else
i = i + 1
End If
Next

End Sub

谢谢!

解决方案

如果您要删除行,那么您应该始终从下到上工作:

  Sub DeleteRows()

Dim rng As Range
Dim counter As Long,numRows as long

With ActiveSheet
Set rng = Application.Intersect(.UsedRange,.Range(C:C))
结束
numRows = rng.Rows.Count

对于counter = numRows到1步骤-1
如果不是rng.Cells(counter)像AA *那么
rng.Cells(counter).EntireRow.Delete
End If
Next

End Sub


I'm close to figuring out the correct way to do this but I'm all searched out. I'm trying to search a column (Asset Tag) that holds various garbage but want to keep entire rows that start with AA as this confirms the row is actually an Asset Tag.

The code below gives me an "Object Required" error, and I believe I might not be correctly telling it to look at cell values with the"If rng.Cells(i) <> Left(cell.Value, 2) = "AA" Then" statement. Can someone point me in the right direction of what I need to do?

Sub DeleteRows()

Dim rng As Range
Dim i As Double, counter As Double
    Set rng = Range("C:C")
    i = 1
    For counter = 1 To rng.Rows.count
    If rng.Cells(i) <> Left(cell.Value, 2) = "AA" Then
        rng.Cells(i).EntireRow.Delete
    Else
        i = i + 1
    End If
Next

End Sub

Thanks!

解决方案

If you're deleting rows then you should always work from the bottom up:

Sub DeleteRows()

    Dim rng As Range
    Dim counter As Long, numRows as long

        With ActiveSheet
           Set rng = Application.Intersect(.UsedRange, .Range("C:C"))
        End With
        numRows = rng.Rows.Count

        For counter = numRows to 1 Step -1
         If Not rng.Cells(counter) Like "AA*" Then
            rng.Cells(counter).EntireRow.Delete
         End If
       Next

End Sub

这篇关于根据标准删除Excel中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:02
查看更多