简体   繁体   English

根据相邻单元格值删除行-VBA

[英]Delete rows based off adjacent cell value- VBA

I'm having trouble with this bit of code to delete rows that don't represent an actual minimum. 我在这段代码上遇到麻烦,无法删除不代表实际最小值的行。 It simply does not work, I have hand verified the logic. 它根本行不通,我已经手工验证了逻辑。 What I'm trying to check is if the value of the cell in row 1, column 2 is greater than row 2, column 2. and if that is the case, then delete row 1 entirely. 我要检查的是,第1行,第2列中的单元格的值是否大于第2行,第2列。如果是这种情况,请完全删除第1行。 I have found examples where they loop similarly to this, but look for a certain string value rather than compare the cells value with a logic statement. 我发现了一些示例,它们的循环与此类似,但它们查找某个字符串值,而不是将单元格值与逻辑语句进行比较。 I have done a check to make sure it is referencing the correct worksheet and that I understand how the delete function works. 我已经进行了检查,以确保它引用了正确的工作表,并且我了解删除功能的工作原理。 I have also tested the code with LRow + 1 instead of LRow - 1 . 我还用LRow + 1而不是LRow - 1测试了代码。 What am I doing wrong? 我究竟做错了什么?

Sub rowdelete()
Dim LRow As Long
With Worksheets("WWOutput")
        For LRow = Cells(Rows.Count, 2).End(xlUp) To Cells(7, 2) Step -1
            If Cells(LRow, 2) > Cells(LRow - 1, 2) And Cells(LRow, 2) < 10000 Then
        Rows(LRow).EntireRow.Delete
    End If
 Next LRow
End With
End Sub

Here is some sample data. 这是一些示例数据。

11804.6875
6415.625095
6333.593845
3786.718845
11841.40682

In this case all I want to be left for data is 在这种情况下,我只想保留数据

11804.6875
3786.718845
11841.40682

Thank you ahead of time! 提前谢谢你!

Like they said... 就像他们说的...

Sub rowdelete()
    Dim LRow As Long
    With Worksheets("Sheet1")
        For LRow = .Cells(Rows.Count, 2).End(xlUp).Row To Cells(7, 2).Row Step -1
            If .Cells(LRow, 2) > .Cells(LRow - 1, 2) And (.Cells(LRow, 2) < 10000) Then
                .Rows(LRow).EntireRow.Delete
            End If
        Next LRow
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM