Deleting rows if columns if first x# of characters match in two columns

vba4PTV

New Member
Joined
May 5, 2023
Messages
6
Office Version
  1. 365
Hi, I need help writing a macro to only delete entire rows if the first eleven characters match in columns G and H. In the example below all rows would be deleted except row 3 because P554353-002_SIX does not match P554353-001. This spreadsheet can be a few hundred rows long before running and deleting. Some Column G items contain a _SIX suffix but what needs to match before deleting are the first eleven characters. Sometimes the cells in G and H columns are blank or a different alphanumeric string and need to stay.

1683297238586.png


Much appreciated!
vba4PTV
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
VBA Code:
Sub RemoveMatch()
    lr = Range("H" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If Trim(Range("G" & i).Value) <> "" And Trim(Range("H" & i).Value) <> "" And Left(Range("G" & i).Value, 11) = Left(Range("H" & i).Value, 11) Then
            Range("A" & i).EntireRow.Delete
        End If
    Next i
End Sub
 
Upvote 0
Awesome thank you iggydarsa!

Could the same be done, but only for P#s greater than P549999?
 
Upvote 0
I havent tested it but you can give it a try:

VBA Code:
Sub RemoveMatch()
    lr = Range("H" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If Trim(Range("G" & i).Value) <> "" And Trim(Range("H" & i).Value) <> "" And Left(Range("G" & i).Value, 11) = Left(Range("H" & i).Value, 11) And Mid(Range("H" & i).Value, 2, 6)>549999 Then
            Range("A" & i).EntireRow.Delete
        End If
    Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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