Macro to remove rows where the difference between to cells is more than 0.05

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,

I have a sheet of data,

In Column "N" in have last years premium.
In Column "R" in have this years premium.

I want to have a macro remove all the data where the premiums are the same.
but to give a leeway of 0.05 or 5p as last year it might have been sold at £456.95 and this year £456.99 for example

So the macro should do this:

Start at Row2 to last row.
if column "N" = Column "R" give or take 0.05 then delete row

Please help if you can

Thanks

Tony
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Are you looking for it to compare each cell in the same row between the columns? For example, you are looking to compare cell N5 to cell R5 and if the difference is not within 5% you delete the 2 cells?
 
Upvote 0
I think you have your title backwards:
Macro to remove rows where the difference between to cells is more than 0.05
From your description, it sounds like you want to remove the rows when the difference is less than or equal to 0.05, not greater than.

Assuming that is correct, try this code:
Code:
Sub MyRemoveRows()

    Dim lastRow As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column N with data
    lastRow = Cells(Rows.Count, "N").End(xlUp).Row
    
'   Loop through all rows, backwards, up to row 2 (first row of data)
    For r = lastRow To 2 Step -1
'   See if difference is more than 0.05
        If Abs(Round(Cells(r, "N"), 2) - Round(Cells(r, "R"), 2)) <= 0.05 Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi Joe,
You are correct sorry for that :)
code looks great thanks

Tony
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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