Simple For循环通过最后一行定义的指定范围

时间:2015-08-24 17:22:27

标签: excel loops delete-row

我正在尝试一个不起作用的相对简单的代码。我试图循环指定的范围并删除第一列的单元格不等于另一个特定单元格的每一行。循环应迭代从单元格“A11”开始的范围,并在LR处完成,或反过来。我尝试了最后一行的许多不同变体以及for循环。我无法理解为什么这么简单的代码不起作用。代码如下:

Dim sht5 As Worksheet
Dim LR As Long
Set sht5 = ThisWorkbook.Sheets("Summary")
LR = Cells(Rows.Count, 1).End(xlUp).Row
With sht5
For i = LR To Range("A11") step -1
    If i <> Range("B9").Value Then
        Rows(i).Delete shift:=xlUp
    End If
Next i
End With

1 个答案:

答案 0 :(得分:1)

试试这个:

Dim sht5 As Worksheet
    Dim LR As Long
    Set sht5 = ThisWorkbook.Sheets("Summary")
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    With sht5
    For I=LR to 11 step -1
    if cells(i,1).value<>range("B9").Value Then
            Rows(i).Delete shift:=xlUp
        End If
    Next i
    End With