0

如果相邻单元格大于所选单元格,我需要删除一行。本质上,如果 J 中的一个单元格大于 K 中的相邻单元格,我希望删除该行。因此,如果 J100 > K100,则应删除第 100 行中的所有数据。我目前能够使用固定值进行处理,因此如果 J100 > 0 但不能使用不断变化的值。对于固定值,我有:

    Sub Delete_Row_Based_On_Bigger_Smaller_Value()

Dim ws As Worksheet

  ' Dont this bit, unless editted on a differently named sheet in which case replace sheet name with relevant sheet
  Set ws = ThisWorkbook.Worksheets("Data Set")
  ws.Activate

  ' Apply Filter. Edit the "ws.Range" with the letter you want to filter. Edit Criterial with what you want to delete. Dont touch the AutoFilter
  ws.Range("Q2:Q143691").AutoFilter Field:=1, Criteria1:="<400000"
  
  ' Delete Rows. Edit the "ws.Range" to be consistant with the above. Dont touch anything else
  Application.DisplayAlerts = False
    ws.Range("Q2:Q143691").SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True
  
  ' Clearing Filter. Dont touch this
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0

End Sub

任何帮助将不胜感激,谢谢!

4

1 回答 1

0

假设我们从:

在此处输入图像描述

我们从底部开始检查每一行并向上工作:

Sub RowKiller()
    Dim i As Long, N As Long
    
    N = Cells(Rows.Count, "J").End(xlUp).Row
    For i = N To 2 Step -1
        If Cells(i, "J").Value > Cells(i, "K").Value Then
            Cells(i, "J").EntireRow.Delete
        End If
    Next i
End Sub

要得到:

在此处输入图像描述

您应该能够使这种方法适应您的编码。

于 2020-08-19T12:06:02.630 回答