简体   繁体   English

如何根据相邻单元格中的值删除单元格

[英]How to delete cell based of value in adjacent cell

I need to delete a row if the adjacent cell is larger than the selected one.如果相邻单元格大于所选单元格,我需要删除一行。 Essentially, if a cell in J is larger then the adjacent cell in K, I want the row to be deleted.本质上,如果 J 中的一个单元格大于 K 中的相邻单元格,我希望删除该行。 So if J100 > K100, all data in row 100 should be deleted.I am currently able to do it with stationary value, so if J100 > 0 but not with a constantly shifting one.因此,如果 J100 > K100,则应删除第 100 行中的所有数据。我目前能够使用固定值进行处理,因此如果 J100 > 0 但不能使用不断变化的值。 for stationary values, I've got:对于固定值,我有:

    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

Any help will be greatly appreciated, Thank you!任何帮助将不胜感激,谢谢!

Say we start with:假设我们从:

在此处输入图像描述

We examine each row starting at the bottom and work upwards:我们从底部开始检查每一行并向上工作:

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

To get:要得到:

在此处输入图像描述

You should be able to adapt this approach to your coding.您应该能够使这种方法适应您的编码。

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

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