VBA删除行

VBA删除行,vba,excel,Vba,Excel,我正在尝试为excel编写一个VBA代码,该代码根据同一列中某些单元格的内容删除行。我现在的程序如下所示: Sub removeCol() Dim i As Integer i = 1 Do While i < 10 If (Cells(i, 2).Text = "a") Then Rows(i).Delete i = i + 1 Else i = i + 1 End If Loop End Sub 我将非常感谢任何提示!现在,这件事根本没有任何影响。其中一个变体是: Sub remov

我正在尝试为excel编写一个VBA代码,该代码根据同一列中某些单元格的内容删除行。我现在的程序如下所示:

Sub removeCol()
Dim i As Integer
i = 1
Do While i < 10
If (Cells(i, 2).Text = "a") Then
Rows(i).Delete
i = i + 1
Else
i = i + 1
End If
Loop
End Sub

我将非常感谢任何提示!现在,这件事根本没有任何影响。

其中一个变体是:

Sub removeCol()
    Dim i As Integer

    For i = 10 To 1 Step -1
        If Cells(i, 2) = "a" Then
            Rows(i).Delete
        End If
    Next
End Sub
Sub test()
    Dim i&, x&
    i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row 'get last row in "B" column
    For x = i To 1 Step -1
        If LCase(Cells(x, 2).Value2) = "a" Then Rows(x).Delete 'Lcase to remove case sensivity, due to "a" not equal to "A"
    Next x
End Sub
如果要使用contains方法检查单元格内容,则可以使用以下方法:

Sub test2()
    Dim i&, x&
    i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row
    For x = i To 1 Step -1
        If LCase(Cells(x, 2).Value2) Like "*a*" Then Rows(x).Delete
    Next x
End Sub
或者这个:

Sub test3()
    Dim i&, x&
    i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row
    For x = i To 1 Step -1
        If InStr(1, Cells(x, 2).Value2, "a", vbTextCompare) > 0 Then Rows(x).Delete
    Next x
End Sub

其中一种变体是:

Sub test()
    Dim i&, x&
    i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row 'get last row in "B" column
    For x = i To 1 Step -1
        If LCase(Cells(x, 2).Value2) = "a" Then Rows(x).Delete 'Lcase to remove case sensivity, due to "a" not equal to "A"
    Next x
End Sub
如果要使用contains方法检查单元格内容,则可以使用以下方法:

Sub test2()
    Dim i&, x&
    i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row
    For x = i To 1 Step -1
        If LCase(Cells(x, 2).Value2) Like "*a*" Then Rows(x).Delete
    Next x
End Sub
或者这个:

Sub test3()
    Dim i&, x&
    i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row
    For x = i To 1 Step -1
        If InStr(1, Cells(x, 2).Value2, "a", vbTextCompare) > 0 Then Rows(x).Delete
    Next x
End Sub

第一:你必须从列表的底部到顶部。假设第5排和第6排有两个a。当您自上而下时,您将首先找到第5行并将其删除。因此,所有行都将移到顶部一行。。。因此,第6行将成为新的第5行。但在下一个循环中,您将检查新的第6行,即第一次删除之前的第7行。看到了吗?第二,不太重要,最好用在下一个,以防循环中有计数器。要做到这一点有很多方法。首先,你必须从列表的底部到顶部。假设第5排和第6排有两个a。当您自上而下时,您将首先找到第5行并将其删除。因此,所有行都将移到顶部一行。。。因此,第6行将成为新的第5行。但在下一个循环中,您将检查新的第6行,即第一次删除之前的第7行。看到了吗?第二,不太重要,最好用在下一个,以防循环中有计数器。实现这一点的方法很多。谢谢!我不确定我是否真的喜欢你的帖子,但这是非常有用的。你可能希望把它比作If LCaseCellsi,2.Value2=a。它目前区分大小写。谢谢!我不确定我是否真的喜欢你的帖子,但这是非常有用的。你可能希望把它比作If LCaseCellsi,2.Value2=a。它目前区分大小写。