Compare Sheet and Columns

doku

New Member
Joined
May 20, 2007
Messages
22
I am not sure if its a duplicate post.
I tried searching the forum, but couldn't relate it to the scenario I needed.
Book1
ABCD
1Sheet1
2NameAge
3a10
4b20
5c30
6d40
7e50
8
9Sheet2
10NameAge
11b20
12c30
13d40
14
15Sheet3
16NameAge
17anotfound
18bmatched
19cmatched
20ddidntmatch
21enotfound
Sheet1


I have three sheet. named Sheet1, Sheet2 and Sheet3.
Sheet1 being the master sheet. What I want to achieve is:
I want to search for Name in sheet1 with the list of names in Sheet2,
If the name is found, then compare the Age in Sheet1 and Sheet2. If the age matches make an entry in Sheet3 "matched" else "didnt match".
If the name itself is not found on sheet2 , make an entry saying not found.
I am not sure what would be the best approach to solve the problem(formula/VB)...
Hope I am clear with my query...
Can you all please help ?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Is there any possibility like Same name with different ages, one matched and one doesn't match....
 
Upvote 0
Code:
Sub test()
Dim a, b, i as Long, w(), e
a = Sheets("sheet1").range("a1").currentregion.resize(,2).value
With createobject("scripting.dictionary")
    .comparemode = vbtextcompare
    For i = 2 To ubound(a, 1)
        .item(a(i, 1)) = Array(a(i, 2), i)
    Next
    b = Sheets("sheet2").range("a1").currentregion.resize(, 2).value
    For i = 2 to UBound(b, 1)
        If .exists(b(i, 1)) then
            w = .item(b(i, 1))
            a(w(1), 2) = IIf(w(0) <> b(i, 2),"not ","") & "matched
            .remove b(i, 1)
        End if
    Next
    if .count > 0 then
        for each e in .items
            a(e(1), 2) = "not found"
        next
    end if
End with
Sheets("sheet3").cells(1).resize(ubound(a,1), 2).value = a
End Sub
 
Upvote 0
In Sheet 3 Cell B2, type this formula (Where A2 contains the name)
=IF(ISERROR(IF(VLOOKUP(A2,Sheet1!A:B,2,FALSE)=VLOOKUP(A2,Sheet2!A:B,2,FALSE),"matched","didn't match")),"not found",IF(VLOOKUP(A2,Sheet1!A:B,2,FALSE)=VLOOKUP(A2,Sheet2!A:B,2,FALSE),"matched","didn't match"))
Copy Down

If you accept #N/A as "not found". you may simply the formula as
=IF(VLOOKUP(A2,Sheet1!A:B,2,FALSE)=VLOOKUP(A2,Sheet2!A:B,2,FALSE),"matched","didn't match")
 
Upvote 0
I checked the formula too .... works great....
Just trying to figureout the formula and its working !!! ;)

The only thing that the formula is unable to achieve is the name column....
Thanks !!!
 
Last edited:
Upvote 0
what do you mean by name column?
btw, the logic is simple:
Lookup the value (name) of Sheet 3 in both Sheet1 and Sheet2. If the name is not avaliable in either Sheet1 or Sheet2, it returns #N/A
If both Sheet1 and Sheet2 contain the same name, the Age(lookup value) is compared and corresponding result returns
 
Upvote 0
Yes.... I got that....
The only confusion that I have now is....
If I want to compare a column index 5 in sheet1 and column index 8 in sheet2 and the result to be palced in column index 5 in sheet3.
Is that possible ?
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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