Skip to content

Instantly share code, notes, and snippets.

@HerbFargus
Last active May 1, 2017 17:50
Show Gist options
  • Save HerbFargus/d6c6cc6d4fee157aad837482b0f8634d to your computer and use it in GitHub Desktop.
Save HerbFargus/d6c6cc6d4fee157aad837482b0f8634d to your computer and use it in GitHub Desktop.
VBA (Excel): Compare two columns and if they match, delete the whole row
' Excel VBA Macro: This compares two columns, if they match the the row is deleted.
Sub Main()
With Application ' Optimise so deleting rows doesnt hang
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
' Set up prompt input boxes for user to choose the columns
Column1 = InputBox("Choose the first column to compare...")
If Column1 = "" Or DelimitedColumn1 Like "*[!A-Za-z]*" Then Exit Sub
Column2 = InputBox("Choose the second column to compare...")
If Column2 = "" Or DelimitedColumn1 Like "*[!A-Za-z]*" Then Exit Sub
LastRow = Range("A" & Rows.Count).End(xlUp).Row ' finding last row used
For i = LastRow To 1 Step -1 ' iterating from last row to 1st
If Range(Column1 & i).Value = Range(Column2 & i).Value Then Rows(i).EntireRow.Delete ' comparing columns 1 and 2
Next
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.CutCopyMode = False
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment