Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 211
Default Sheet Compare Macro...

Sheet 1 contains 10,000 customers all uniquely identified by a customer
number in column A, with extra details stretching to column Z. Sheet 2
contains up to 10,000 customer details that are identical to sheet 1 in terms
of customer number, but their additional details might have been changed. I
need a macro that will compare sheet 1 and sheet 2 by customer number. If any
row on sheet 2 is different to sheet 1 then the changed data sheet 2 will be
transferred into sheet 1. Make sense?

This has been driving me nuts...any help appreciated...

Gordon.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Sheet Compare Macro...

How about something like 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))
End With

LastCol = 26 'A to Z
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"
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"
Else
'already in the master
'don't do anything
End If
End With
Next myCell

Application.ScreenUpdating = True

End Sub



It removes the fill color and marks the differences in red (.colorindex =3).

It also adds an indicator to column AA with the change type.

And if you added more records in Sheet2, it'll add them to the bottom and mark
them "added".

And if the key was missing from the other sheet, it puts a message there, too.

Make sure you try it out against a test copy of your workbook--just in case (or
close without saving if it breaks things too much!).





Gordon wrote:

Sheet 1 contains 10,000 customers all uniquely identified by a customer
number in column A, with extra details stretching to column Z. Sheet 2
contains up to 10,000 customer details that are identical to sheet 1 in terms
of customer number, but their additional details might have been changed. I
need a macro that will compare sheet 1 and sheet 2 by customer number. If any
row on sheet 2 is different to sheet 1 then the changed data sheet 2 will be
transferred into sheet 1. Make sense?

This has been driving me nuts...any help appreciated...

Gordon.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare Two Sheet adeel via OfficeKB.com Excel Discussion (Misc queries) 3 November 30th 08 04:31 PM
Compare two data in same sheet Sabbah Excel Worksheet Functions 0 November 30th 07 05:21 PM
Compare data in one sheet to another Not Excelerated[_2_] Excel Worksheet Functions 1 June 1st 07 05:30 PM
Help me, compare 2 sheet and extract the match data into the new sheet. sweetnet Excel Discussion (Misc queries) 1 February 22nd 06 07:49 PM
Compare Sheet Cell to Sheet Column Brenda Excel Worksheet Functions 2 January 4th 06 07:32 PM


All times are GMT +1. The time now is 10:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"