VBA 2 matching collumn criteria in two sheets (preferably seperate workbooks but same worksheet works too)

Enygma88

New Member
Joined
Apr 28, 2015
Messages
1
I am looking for a way to compare columns A and B in one sheet to that of column A and B in another sheet. If BOTH column A and B in the first sheet match column A and B in the second sheet - I would like to have that entire row deleted in the first sheet. The sheets are in different workbooks but moving the sheet from one to the other so that they are in the same workbook is not an issue.

The purpose of deleting rows where there is matching columns is that this is a comparison from a monthly sheet to the master sheet that will determine who is a new client. Both sheets will have the same layout just different names/data (or the same name and data) screenshot example below of what the sheet layout would look like in both sheets:

[TABLE="width: 336"]
<colgroup><col><col><col><col><col></colgroup><tbody>[TR]
[TD]First Name[/TD]
[TD]Last Name[/TD]
[TD]Phone Number[/TD]
[TD]Address[/TD]
[TD]Email[/TD]
[/TR]
[TR]
[TD]Jane[/TD]
[TD]Doe[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[/TR]
[TR]
[TD]David[/TD]
[TD]Smith[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[/TR]
[TR]
[TD]Jessica[/TD]
[TD]Roberts[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[TD]Data
[/TD]
[/TR]
[TR]
[TD]Bruce[/TD]
[TD]Wayne[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[/TR]
[TR]
[TD]Angelina[/TD]
[TD]Jolee[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[TD]Data[/TD]
[/TR]
</tbody>[/TABLE]

I have the below code but it only compares one column, and I need to compare two (there may be multiple clients with the name Jane or Smith, so I need to compare two columns)

Sub monmon()
Dim S1 As Worksheet, S2 As Worksheet, lR1 As Long, lR2 As Long, _
dRws As Range, c As Range, n As Long
Set S1 = Sheets("Random1")
Set S2 = Sheets("Sheet2")
lR1 = S1.Range("A" & Rows.Count).End(xlUp).Row
lR2 = S2.Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For Each c In S1.Range("A2", "A" & lR1)
If Not IsEmpty(c) Then
n = 0
On Error Resume Next
n = Application.Match(c.Value, S2.Range("A2", "A" & lR2), 0)
On Error GoTo 0
If n > 0 Then
If Not dRws Is Nothing Then
Set dRws = Union(dRws, c.EntireRow)
Else
Set dRws = c.EntireRow
End If
End If
End If
Next c
If Not dRws Is Nothing Then dRws.Delete
Set dRws = Nothing
For Each c In S2.Range("A2", "A" & lR2)
If IsEmpty(c) Then
If Not dRws Is Nothing Then
Set dRws = Union(dRws, c.EntireRow)
Else
Set dRws = c.EntireRow
End If
End If
Next c
If Not dRws Is Nothing Then dRws.Delete
End Sub

Any help is appreciated! Thank you.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
The easiest way I can think of is a helper column. Combine the two columns as a string in another column and compare using that.
 
Upvote 0

Forum statistics

Threads
1,223,718
Messages
6,174,082
Members
452,542
Latest member
Bricklin

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