Using VBA to compare 2 columns with another 2 columns

JOhnG2

New Member
Joined
Nov 25, 2015
Messages
9
Hi all,
I'm looking to compare surname and first name columns in one worksheet with surname and first name columns in another sheet. If there is a match it's deleted from the first file only. The user enters the range of data to be inputted.
So far i've been able to work on one column but i'm struggling to work with 2. Any help is appreciated. My code is below.

Sub Compare()
Dim Range1 As Range, Range2 As Range, Rng1 As Range, Rng2 As Range, outRng As Range
Set Range1 = Application.Selection
Set Range1 = Application.InputBox("Range1 :", Type:=8)
Set Range2 = Application.InputBox("Range2:", Type:=8)
Application.ScreenUpdating = False
For Each Rng1 In Range1
xValue = Rng1.Value
For Each Rng2 In Range2
If xValue = Rng2.Value Then
If outRng Is Nothing Then
Set outRng = Rng1
Else
Set outRng = Application.Union(outRng, Rng1)
End If
End If
Next
Next
outRng.EntireRow.Delete
Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi John,

I got a little confused with your post. I get the part about comparing the surname and first name from columns on one sheet to columns on another sheet. I got lost on the part about the user enters the range of data to be inputted. Are you saying the user in going input a certain name to look for on the two sheets or what...

igold
 
Upvote 0
Hi John,

I got a little confused with your post. I get the part about comparing the surname and first name from columns on one sheet to columns on another sheet. I got lost on the part about the user enters the range of data to be inputted. Are you saying the user in going input a certain name to look for on the two sheets or what...

igold

Sorry I mentioned the range just to try and explain my code a little. Don't worry about it.
If you were able to help comparing the surname and first name from columns on one sheet to columns on another sheet it would be great.
 
Upvote 0
JOhnG2,

One approach to comparing 2 columns with another 2 columns is to use the Concatenate function, or its operator "&"...

Code:
[COLOR=#a9a9a9]Sub Compare()
Dim Range1 As Range, Range2 As Range, Rng1 As Range, Rng2 As Range, outRng As Range
[/COLOR]Dim xValue As String[COLOR=#a9a9a9]

[/COLOR]'Set Range1 = Application.Selection[COLOR=#a9a9a9]
Set Range1 = Application.InputBox("Range1:", Type:=8)
Set Range2 = Application.InputBox("Range2:", Type:=8)
Application.ScreenUpdating = False

For Each Rng1 In Range1
    [/COLOR]xValue = Rng1.Value & Rng1.Offset(0, 1).Value[COLOR=#a9a9a9]
    For Each Rng2 In Range2
        [/COLOR]If xValue = Rng2.Value & Rng2.Offset(0, 1).Value Then[COLOR=#a9a9a9]
            If outRng Is Nothing Then
                Set outRng = Rng1
                Else
                Set outRng = Application.Union(outRng, Rng1)
            End If
        End If
    Next
Next

outRng.EntireRow.Delete
Application.ScreenUpdating = True
End Sub[/COLOR]

The code assumes the surname and first names columns are adjacent, and uses the Offset method to specify the adjacent range.

Cheers,

tonyyy
 
Upvote 0
Tonyyy,
That's great. Sometimes you don't see the obvious when it's staring you in the face. Code works a treat. Many thanks.
John
 
Upvote 0
Whilst it most likely won't be an issue for you here, I would advise against direct concatenation like that to do such a comparison.
To demonstrate, consider a case where the names are all in UPPER case

In Range 1 is the name "JO" "EWING". In Range 2 is the very different name "JOE" "WING".
Concatenating them as suggested turns them both into "JOEWING" meaning they would test the same when they are not.

That might be unlikely and perhaps upper/lower cases may also help you out but in general, to avoid the possibility I would suggest a concatenation like

Rng1.Value & "|" & Rng1.Offset(0, 1).Value
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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