Excel 基于多个条件删除行

Excel 基于多个条件删除行,excel,vba,macros,Excel,Vba,Macros,你能帮我做以下代码吗 Sub DeleteRows() Dim c As Range Dim SrchRng Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp)) Do Set c = SrchRng.Find("12345", LookIn:=xlValues) If Not c Is Nothing Then c.EntireRow.Delete Loop While Not

你能帮我做以下代码吗

Sub DeleteRows()
Dim c As Range
Dim SrchRng

Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
Do
    Set c = SrchRng.Find("12345", LookIn:=xlValues)
    If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing
端接头

我在a列中有一个包含程序代码的图表。我想创建一个宏,该宏将删除包含特定程序代码的所有行:12345、541、9099等。 我的代码只能引用一个值。我不知道如何添加更多。除此之外,它还将删除包含“12345”的程序代码。例如,它将删除程序代码为123456的行。我们能阻止它也这样做吗

另外,不确定像我那样设置范围是否是个好主意:A1:A65536。太大了


谢谢大家!

您应该在该范围内迭代。如果没有那么多数据,也不希望将范围设置得那么大

子删除行()
我想我会坚持多久
最后一排的颜色和前面一样长
最后一行=ActiveSheet.Range(“A65536”).End(xlUp).row
对于i=第1步的最后一行-1
如果ActiveSheet.Cells(i,1).Value=“12345”或_
ActiveSheet.Cells(i,1).Value=“541”或_
ActiveSheet.Cells(i,1).Value=“9099”然后
ActiveSheet.Cells(i,1).EntireRow.Delete
如果结束
接下来我
端接头

通过这种方式,您可以使用要在数据中检查的值/字符串来检查数组中的值:

Sub DeleteRows()
    Dim c As Range
    Dim i
    Dim r
    Dim theValues(1 To 5)
    Dim SrchRng As Range

    theValues(1) = "1231"
    theValues(2) = "1232"
    theValues(3) = "1233"
    theValues(4) = "1234"
    theValues(5) = "1235"

    r = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Set SrchRng = Range(Cells(1, 1), Cells(r, 1))
    For Each i In theValues
        Do
            Set c = SrchRng.Find(i, LookIn:=xlValues, LookAt:=xlWhole)
            'see the ", LookAt:=xlWhole" added, this way you can find just the Whole values.
            If Not c Is Nothing Then c.EntireRow.Delete
        Loop While Not c Is Nothing
    Next i
End Sub
编辑#1 当您在评论中询问时,请参阅编辑:查看数据,仅查找完整的值(您查找的是
91
而不是
910
1891
),然后这是我的版本如果您想将值放在工作表中的某个范围内,则可以添加要查找的任何值

Sub DeleteRows()
    Dim c As Range
    Dim i
    Dim r
    Dim rng As Range
    Dim a
    Dim theValues()
    Dim SrchRng As Range

    r = Range("T1").End(xlDown).Row
    Set rng = Range("T1", Cells(r, 20))

    For a = 1 To rng.Count 'in this range i store the values
        ReDim Preserve theValues(1 To a)
        theValues(a) = rng(a)
    Next a

    r = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Set SrchRng = Range(Cells(1, 1), Cells(r, 1))
    For Each i In theValues
        Do
            Set c = SrchRng.Find(i, LookIn:=xlFormulas, LookAt:=xlWhole)
            If Not c Is Nothing Then c.EntireRow.Delete
        Loop While Not c Is Nothing
    Next i
End Sub

请检查我的答案并编辑。谢谢!但是我得到了这四行的语法错误:如果ActiveSheet.Cells(I,1).Value='12345'或ActiveSheet.Cells(I,1).Value='541'或ActiveSheet.Cells(I,1).Value='9099',那么我不确定原因:S@bernieAh,请参见编辑。单引号应该是双引号。这非常有效。非常感谢。我假设代码迭代了这个范围@伯尼,不客气。是的,代码迭代第一列。这非常有效,只是删除了其他行,因为单元格中包含了这些值!有没有一种方法可以使它特定于列a?我喜欢列表样式@埃尔伯特:太好了。谢谢大家!@埃尔伯特