Comparing Data from two 2 worksheets
Record a macro when you change the fill color for 3 cells. Look at the code to
pick out the colors that you want.
Then look for .colorindex (3 times) to change in this:
Option Explicit
Sub testme()
Application.ScreenUpdating = False
Dim MstrWks As Worksheet
Dim NewWks As Worksheet
Dim MstrKeyRange As Range
Dim NewKeyRange As Range
Dim myCell As Range
Dim destCell As Range
Dim LastCol As Long
Dim iCol As Long
Dim res As Variant
Set MstrWks = ActiveWorkbook.Worksheets("sheet1")
Set NewWks = ActiveWorkbook.Worksheets("sheet2")
With MstrWks
Set MstrKeyRange = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
.Cells.Interior.ColorIndex = xlNone 'remove all fill color!
End With
With NewWks
Set NewKeyRange = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
.Cells.Interior.ColorIndex = xlNone
End With
LastCol = 6 'A to F
MstrWks.Columns(LastCol + 1).Clear
For Each myCell In MstrKeyRange.Cells
With myCell
res = Application.Match(.Value, NewKeyRange, 0)
If IsError(res) Then
.Parent.Cells(myCell.Row, LastCol + 1).Value _
= "Not on other sheet"
myCell.EntireRow.Resize(1, LastCol).Interior.ColorIndex = 5
Else
For iCol = 1 To LastCol - 1
If .Offset(0, iCol).Value _
= NewKeyRange(res).Offset(0, iCol).Value Then
'do nothing, they match
Else
' .Offset(0, iCol).Value _
' = NewKeyRange(res).Offset(0, iCol).Value
.Offset(0, iCol).Interior.ColorIndex = 3
.Parent.Cells(myCell.Row, LastCol + 1).Value _
= "Changed"
End If
Next iCol
End If
End With
Next myCell
'check for newly added entries
For Each myCell In NewKeyRange.Cells
With myCell
res = Application.Match(.Value, MstrKeyRange, 0)
If IsError(res) Then
'missing from new workbook!
' With MstrWks
' Set destCell _
' = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
' End With
' .Resize(1, LastCol).Copy _
' Destination:=destCell
destCell.Parent.Cells(destCell.Row, LastCol + 1).Value _
= "Added"
myCell.EntireRow.Resize(1, LastCol).Interior.ColorIndex = 7
Else
'already in the master
'don't do anything
End If
End With
Next myCell
Application.ScreenUpdating = True
End Sub
Roberto R wrote:
Thanks Dave, I tried the first suggestion.
What would be ideal would be to have the words "changed" and "added" on each
row that has changed or been added on the "new" sheet only instead of on the
"old" one whilst having "deleted" on the "old" sheet for any part numbers
which are missing on the "new" sheet.
Also, is it possible to have the cells which have been "changed", "added" or
"deleted" to be coloured in 3 different colors WITHOUT the changes actually
being applied?
I know I'm perhaps asking for a lot but now that I can "smell" the final
goal, I'm inpatient to see it working!
Thanks again
"Dave Peterson" wrote in message
...
You can comment out the lines (put an apostrophe to the far left of that
line)
to make it not do the stuff you don't want it to do.
In this case, I think you only want to comment this section:
For iCol = 1 To LastCol - 1
If .Offset(0, iCol).Value _
= NewKeyRange(res).Offset(0, iCol).Value Then
'do nothing, they match
Else
' .Offset(0, iCol).Value _
' = NewKeyRange(res).Offset(0, iCol).Value
.Offset(0, iCol).Interior.ColorIndex = 3
.Parent.Cells(myCell.Row, LastCol + 1).Value _
= "Changed"
Just add those 2 apostrophes and see if that works.
I'm not sure what should happen to the Added stuff. If you really want
those
gone, you can just comment these lines.
'check for newly added entries
' For Each myCell In NewKeyRange.Cells
' With myCell
' res = Application.Match(.Value, MstrKeyRange, 0)
' If IsError(res) Then
' 'missing from new workbook!
' With MstrWks
' Set destCell _
' = .Cells(.Rows.Count, "A").End(xlUp).Offset(1,
0)
' End With
' .Resize(1, LastCol).Copy _
' Destination:=destCell
' destCell.Parent.Cells(destCell.Row, LastCol + 1).Value _
' = "Added"
' Else
' 'already in the master
' 'don't do anything
' End If
' End With
' Next myCell
But I don't think you'll get the information that you want.
Roberto R wrote:
Hi again,
IT WORKS! Sorry Dave, it was my security setting that was stopping it
from
working.
I noticed it physically changes the data in the "old" sheet to make it
exactly the same as the "new" sheet, colours the cell and adds the words
"changed" or "added" next to the row.
Is it possible for it to highlight cells that reuire changing or have
been
added (or deleted) without it actually doing the changes? That would be
fantastic!
Thanks again
<<snipped
--
Dave Peterson
|