Delete rows if a cell value is less than another cell value

simonarcher

New Member
Joined
Mar 16, 2010
Messages
3
Hi.
I have table with a varying number of rows. (20000+)
If the cell in Col D is less than Col F I want the row deleted and so on until only rows where Col D is greater then Col F remain.
Can anyone help a learner like me with some macro code?
Many thanks in advance.
Simon
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Simon,

Here's one way that deletes the rows backwards to Row 2 (just try it initially on a copy of your data in case the results are not as expected):

Code:
Sub Macro1()

    Dim lngLastRow As Long, _
        lngActiveRow As Long
    
    If WorksheetFunction.CountA(ActiveSheet.Cells) > 0 Then
    
        lngLastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
    Else
        
        MsgBox "There is no data on " & ActiveSheet.Name & "!!", vbExclamation, "Delete Row Editor"
        Exit Sub
                     
    End If
    
    Application.ScreenUpdating = False

    For lngActiveRow = lngLastRow To 2 Step -1
        If Range("D" & lngActiveRow).Value < Range("F" & lngActiveRow).Value Then
            Rows(lngActiveRow).EntireRow.Delete
        End If
    Next lngActiveRow
    
    Application.ScreenUpdating = True
    
End Sub

HTH

Robert
 
Upvote 0
Hi Simon and Welcome to the Board
What happens if D is = to F ??
For your original question, try this
Code:
Sub dlrow()
Dim lr As Long, r As Long
lr = Range("D" & Rows.Count).End(xlUp).Row
    For r = lr To 1 Step -1
        If Range("D" & r).Value < Range("F" & r).Value Then Rows(r).Delete
    Next r
End Sub

If you need to allow for < or = to then use this one
Code:
Sub dlrow()
Dim lr As Long, r As Long
lr = Range("D" & Rows.Count).End(xlUp).Row
    For r = lr To 1 Step -1
        If Range("D" & r).Value < = Range("F" & r).Value Then Rows(r).Delete
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,812
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top