Macro to delete row when value of two cells is not equal

Jibroni

New Member
Joined
Apr 7, 2022
Messages
25
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello,

I have the following table and need a macro that will delete the entire row if the values in columns E (Remaining) and F (Saba Remaining TUs) is not equal. So in this example here:
ID18 - TrainingTraining: TrainingAccount NameExpiration DateRemainingSaba Remaining TUs
a150h000009XncWAASTR2104-7532x7/15/202236
36​
a150h000009Xqc1AACTR2104-7550xx7/23/202240
30​
a150h000009XpJ3AAKTR2104-7543xxx7/20/202230
40​
a150h000009XncgAACTR2104-7534xxxx7/15/202228
28​

Rows 3 and 4 would be deleted because their values do not match.

Additional note: values in columns E and F are always numeric.

Is there a basic macro that would do this? Or maybe even a formula?
Thank you!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try:
VBA Code:
Sub DeleteRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = LastRow To 2 Step -1
        If Cells(x, 5) <> Cells(x, 6) Then
            Rows(x).Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub DeleteRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = LastRow To 2 Step -1
        If Cells(x, 5) <> Cells(x, 6) Then
            Rows(x).Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
Hi @mumps, thanks for the quick reply. I tested your macro on the example table from my original post and it deleted the rows where the values are not equal, but also deleted the 2nd row where the values are equal. Any idea why it would delete that row? Appreciate your help!
 
Upvote 0
I had the same problem when I first tested the macro. It looks like the values in column F have an invisible question at the end so F2 displays as 36 but in reality is 36? This would make all the values different. Are you importing the data from an external source?
 
Upvote 0
I had the same problem when I first tested the macro. It looks like the values in column F have an invisible question at the end so F2 displays as 36 but in reality is 36? This would make all the values different. Are you importing the data from an external source?
Ah, thanks for catching that. Yes, I am importing the data. So that makes sense. Your macro does the trick, I just need to investigate the data from the import first, before I run the macro. Thank you for your help!
 
Upvote 0
Here is an alternative approach that doesn't require a loop:

VBA Code:
Sub TestDelete()
'
    Dim LastRowInColumn As Long
'
    LastRowInColumn = Range("E" & Rows.Count).End(xlUp).Row
'
    Application.ScreenUpdating = False
'
    On Error GoTo NoDeletes
    With Range("E2:E" & LastRowInColumn)
        .Value = Evaluate("If(" & .Address & "-" & .Offset(0, 1).Address & "<>0,""#N/A""," & .Address & ")")
        .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    End With
'
NoDeletes:
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here is an alternative approach that doesn't require a loop:

VBA Code:
Sub TestDelete()
'
    Dim LastRowInColumn As Long
'
    LastRowInColumn = Range("E" & Rows.Count).End(xlUp).Row
'
    Application.ScreenUpdating = False
'
    On Error GoTo NoDeletes
    With Range("E2:E" & LastRowInColumn)
        .Value = Evaluate("If(" & .Address & "-" & .Offset(0, 1).Address & "<>0,""#N/A""," & .Address & ")")
        .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    End With
'
NoDeletes:
    Application.ScreenUpdating = True
End Sub
Ah, this also does the trick. Thank you!
 
Upvote 0
You are welcome.

Not sure how many rows you are using this on, but if you had a large amount of rows to check, the no loop version would probably perform faster.
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,292
Members
449,218
Latest member
Excel Master

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