Macro to Delete Rows Based on Matching Columns

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
Hello all,

Trying to write a macro that will compare two lists, then delete rows based on matching and non-matching results.

I have two sheets: sheet A and sheet B. If a title on sheet A is not found on sheet B, then delete that title on sheet A.

On sheet A, the list is in column 1. However, in sheet B, the titles are under the column labeled “Title” which can be in any column. This is why I’d like to use the header in row 1 to identify this column.

For example:
Sheet A:
<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--> <table class="MsoNormalTable" style="width: 48pt; margin-left: 4.65pt; border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0" width="64"> <tbody><tr style="height: 12.75pt;"> <td style="width: 48pt; border: 1pt solid windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title1
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title2
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title3
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title4
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title5
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title6
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title7
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title8
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title9
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title10
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title11
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title12
</td> </tr> </tbody></table>

Sheet B:
<table class="MsoNormalTable" style="width: 48pt; margin-left: 4.65pt; border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0" width="64"><tbody><tr style="height: 12.75pt;"><td style="width: 48pt; border: 1pt solid windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap">Title
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title1
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title2
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title4
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title6
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title7
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title8
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title11
</td> </tr> </tbody></table>

Sheet A after macro:
<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><table class="MsoNormalTable" style="width: 48pt; margin-left: 4.65pt; border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0" width="64"><tbody><tr style="height: 12.75pt;"><td style="width: 48pt; border: 1pt solid windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap">Title
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title1
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title2
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title4
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title6
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title7
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title8
</td> </tr> <tr style="height: 12.75pt;"> <td style="width: 48pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; padding: 0in 5.4pt; height: 12.75pt;" valign="bottom" width="64" nowrap="nowrap"> Title11
</td> </tr> </tbody></table>

The row containing the extra Titles on sheet A are deleted because these titles are not found on sheet B. Any help is most appreciated! Thank you.




 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this (assumes the sheet names are A and B and the Titles in sheet A begin in cell A1 (adjust to suit):
Code:
Sub RemoveTitles()
Dim wsA As Worksheet, wsB As Worksheet, rngA As Range, rngB As Range
Dim lRwA As Long, delRws As Range, ct As Long

Set wsA = ThisWorkbook.Sheets("A")
Set wsB = ThisWorkbook.Sheets("B")
lRwA = wsA.Range("A" & Rows.Count).End(xlUp).Row
Set rngA = wsA.Range("A1", "A" & lRwA)
Set rngB = wsB.Cells.Find("Title")
Set rngB = Range(rngB, rngB.End(xlDown))
For Each c In rngA
    If WorksheetFunction.CountIf(rngB, c.Value) = 0 Then
        ct = ct + 1
        If delRws Is Nothing Then
            Set delRws = c
        Else
            Set delRws = Union(delRws, c)
        End If
    End If
Next c
With delRws
    .Delete
End With
Select Case ct
    Case 0
        MsgBox "No titles in A were not found in B"
    Case Is > 0
        MsgBox ct & " Titles in A were not found in B and were deleted from A"
End Select
        

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

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