Solved

Excel Comparison of 2 sheets

Posted on 2012-03-19
7
187 Views
Last Modified: 2012-03-19
I have two sheets

sheet1 has col1, col2, col3, col4

and

Sheet2 has col1, col2, col3, col4

The data in both the sheets is sorted as col2, col1

I want to run a comparison on two sheets, I want find the records missing in sheet2 that are present in sheet 1

We need to comapre col2 and the col1

Example of data in sheet 1

col1, col2, col3, col4
a.b    d         1      1
d.c    d          2      1
E.F    D
w.s   e    
e.d    e           1     1


Example of sheet2
col1, col2, col3, col4
a.b    d         1      1
w.s   e    


FINAL SHEET1 SHOULD LOOK LIKE THIS
col1, col2, col3, col4  COL5
a.b    d         1      1
d.c    d          2      1     MISSING
E.F    d                         MISSING
w.s   e    
e.d    e           1     1    MISSING




if sheet1.col2row1=sheet2.col2ROW1
THEN CHECK TO SEE IF SHEET1.COL1.ROW1 (loop through sheet2.col1.rows till the col2 is same...)

No idea how to do it in excel.
0
Comment
Question by:SMadhavi
  • 4
  • 2
7 Comments
 
LVL 12

Expert Comment

by:kgerb
ID: 37738955
SMadhavi,
Try this macro out.  I think it will do what you want.  If not let me know and we can change it.  Make a copy of your workbook and try it on the copy just to make sure nothing gets overwritten.

I pasted your sample data into Sheet1 and Sheet2 starting in cell A1 in both sheets.

Sub CompareSheets()
Dim r As Range, c As Range, sAdd As String
For Each r In Range("B1", Cells(Rows.Count, "B").End(xlUp))
    With Worksheets("Sheet2").Range("B:B")
        Set c = .Find(r, LookIn:=xlValues)
            If Not c Is Nothing Then
                sAdd = c.Address
                Do
                    If c.Offset(, -1) <> r.Offset(, -1) Then r.Offset(, 3) = "Missing"
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> sAdd
           End If
    End With
Next r
End Sub

Open in new window

Kyle
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37739016
If VBA is not a must then this is a better way. Enter this formula in E1 or any other column and then copy it down.
0
 

Author Comment

by:SMadhavi
ID: 37739464
tHE FIRST SUB JUST FINDS THE FIRST ALL OTHER VALUES SHOW AS MISSING
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:kgerb
ID: 37739573
hmmm....It worked for your test data.  Can you upload a sample file with more data so I can test it.

EDIT:
Never mind.  I see the problem.  I will have a new one shortly.
0
 
LVL 12

Accepted Solution

by:
kgerb earned 500 total points
ID: 37739624
Try this one.  Seems to work better.

Kyle
Sub CompareSheets()
Dim r As Range, c As Range, sAdd As String, bMissing As Boolean
For Each r In Range("B1", Cells(Rows.Count, "B").End(xlUp))
    bMissing = True
    With Worksheets("Sheet2").Range("B:B")
        Set c = .Find(r, LookIn:=xlValues)
            If Not c Is Nothing Then
                sAdd = c.Address
                Do
                    If c.Offset(, -1) = r.Offset(, -1) Then bMissing = False
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> sAdd
                If bMissing Then r.Offset(, 3) = "Missing"
           End If
    End With
Next r
End Sub

Open in new window

0
 

Author Comment

by:SMadhavi
ID: 37739685
Yes this works exactly the way I want. Thanks
0
 
LVL 12

Expert Comment

by:kgerb
ID: 37739694
You're welcome.  Glad to help.

Kyle
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question