Solved

Highlight Difference between two Sheets in the same workbook?

Posted on 2011-03-25
14
645 Views
Last Modified: 2012-05-11
I have an interesting problem I think. I need to develop a VBA code to compare two Excel worksheets to find the differences, if any, for each store number listed on the current tab. As you can see from the abbreviated sample attached they are in random order and not all store numbers are listed on both sheets. This would be a simple manual process if not comparing 1500 lines and 85 columns of information.

I also need to highlight the entire line of data if a store number is not found on last weeks report tab.


Highlight-Differences-Sample.xlsx
0
Comment
Question by:bearblack
  • 7
  • 4
  • 3
14 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 35216219
Step one: Concatenate all data for each row for both sheets.
Step two: Search each row in sheet1 for a corresponding row in sheet2 and highlight as required
Step three: Search each row in sheet2 for a corresponding row in sheet1 and highlight as required
Step four: Delete/Erase the concatenated fields

Leon
0
 
LVL 2

Author Comment

by:bearblack
ID: 35217627
I need to locate not only the store that has data changed but which cell in the store row has changed
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 35217685
I need to locate not only the store that has data changed but which cell in the store row has changed

Why? You wanted to highlight the whole row in your question? Besides, how will you know which row should be matched to which other row for the cell to cell comparision without any kind of an identifier?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:bearblack
ID: 35217850
The store # is the identifier
0
 
LVL 2

Author Comment

by:bearblack
ID: 35217862
and I said Highlight the changes(I need to locate not only the store that has data changed but which cell in the store row has changed) BUT highlight the entire line of data if a store number is not found on last weeks report tab.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 35218065
The store # is the identifier

That is not enough since its not unique. For example Store #20 occurs 2 times on the 'Last Week' tab but once on the 'Cuerrent' tab, how would the application know which should it be matched vs?
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 35227642
This code is based on what I have understood. This will compare each of current week row with the first matching row from Last week.

Saqib
Sub hiliteDiff()
Dim cw As Worksheet
Dim lw As Worksheet
Set cw = Worksheets("Cuerrent")
Set lw = Worksheets("Last week")
cw.UsedRange.Offset(1, 0).Interior.Pattern = xlNone
For Each rs In cw.UsedRange.Rows
If rs.Cells(1, 1) <> "" Then
mr = Application.Match(rs.Cells(1, 1), lw.Range("A:A"), 0)
If IsError(mr) Then
rs.Interior.Color = 65535
Else
For i = 2 To 4
If rs.Cells(1, i) <> lw.Cells(mr, i) Then rs.Cells(1, i).Interior.Color = 255
Next i
End If
End If
Next rs
End Sub

Open in new window

0
 
LVL 2

Author Comment

by:bearblack
ID: 35237720
Sorry about the dup -- On the working file the store is unique and will only be listed once.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 35238023
Did you try the code?
0
 
LVL 2

Author Closing Comment

by:bearblack
ID: 35242480
Worked Perectly
0
 
LVL 2

Author Comment

by:bearblack
ID: 35242495
Could you please take sometime to explain the code for me?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 35243786
With pleasure

Sub hiliteDiff()
Dim cw As Worksheet
Dim lw As Worksheet
Set cw = Worksheets("Cuerrent")
Set lw = Worksheets("Last week")

'*****remove all colors from current week sheet
cw.UsedRange.Offset(1, 0).Interior.Pattern = xlNone

'**** loop through each row in the current week worksheet
For Each rs In cw.UsedRange.Rows
    '*****This section only takes care of non-blanks in the first column of the current week worksheet
    If rs.Cells(1, 1) <> "" Then
        '******this checks if there is a row in last week which matches a row in current week
        mr = Application.Match(rs.Cells(1, 1), lw.Range("A:A"), 0)
       
        '******if there is no match then mr contains an error in which case the entire row is given color 65535(yellow)
        If IsError(mr) Then
            rs.Interior.Color = 65535
        '*****if there is a match (ie mr does not have an error) then it checks individual cells (columns 2 to 4)
        Else
            For i = 2 To 4
                '*****if the individual cells do not match then the cell is colored 255(red)
                If rs.Cells(1, i) <> lw.Cells(mr, i) Then rs.Cells(1, i).Interior.Color = 255
            Next i
        End If
    End If
Next rs

End Sub
0
 
LVL 2

Author Comment

by:bearblack
ID: 35244068
Each rs In cw.UsedRange.Rows
 The "rs" is what is throwing me along with  rs.Cells(1, 1) which seems to be written as a worksheet.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 35244292
rs is a row in the current worksheet. In the first instance I intended to type a rw but ended up in rs accidentally. Just used it as-is instead of correcting it.

when I use rs.cells(1,1) it considers rs to be a worksheet with number of rows equal to 1 and the number of columns equal to the number of columns used int the worksheet (cw.usedrange)

Saqib
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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