Compare 2 spreadsheets and highlight differences

1cyril1

Board Regular
Joined
Mar 31, 2003
Messages
89
I did a search for compare threads and there are numerous posts but I didn't manage to find one of the more simpler tasks covered - I'm sure they are hiding in there somewhere! If you can point me towards one that meets my needs or if you can suggest a solution via Reply post I'd be very grateful!

I've 2 spreadsheets as follows:

A) cols A to Z (a master doc that requires updating weekly)
B) cols A to G (updates downloaded from a work database) - all 7 cols have equivalent cols in sheetA though not adjacent

One of the cols in A and B contains the key field that's used to check for a match between the 2 sheets (in SheetA it's Col B and in SheetB it's Col A).

There could be over 4,000 records/rows in SheetA but SheetB will typically have less than 500 rows. I want to run a compare between the 2 sheets weekly and:


  1. Check each record in SheetB (ColA) against all of the records in SheetA (ColB) based on the key field.
  2. If there's no matching key in the weekly download (SheetB-ColA), anywhere in SheetA (ColB), then there is no updating to be done for that row and move on to the next record in B.
  3. Where a match is found on the key field in SheetA and SheetB, then the row in SheetB is to be highlighted (ideally the rows would be copied to a new worksheet as a record of that week's changes). Then, the other 6 fields in SheetB would replace the corresponding 6 fields in SheetA, even if only one of the fields is different. For the 6 non-key fields, let's assume that SheetB_Cols B to G correspond with SheetA_Cols C, E, G, I, K, M

I hope I've explained what's required reasonably clearly. I've used the terms record/row and field/cell interchangeably. The spreadsheets have yet to be created so I can't provide samples. However, I know the cols to be matched won't appear in the same order and I've used an illustrative example. To summarise the above:

If cell A1 in SheetB matches any ColB cell in Sheet A, then the data in B1, C1, D1, E1, F1 and G1 is highlighted and cells in the corresponding SheetA row are replaced by the SheetB data.

I look forward to any guidance you may be able to provide.

Many Thanks!
Cyril
 
Thanks Bob, I'm just back from a short break and having a very quick look at home (on my Mac!). I just changed 3 cells in one row in todaydata and they highlighted fine in both workbooks when I tried your macro - great, that's what I wanted! It bombed out near the end on this "Sheets(2).Select" with a 'subscript out of range' error. This might be a small issue hopefully! (can I ignore the AA:AK area as some temp working space?).

P.S. a non-match is a row in todaydata that (a) doesn't have a key-field match in yesterday or (b) finds a match but one or more of the cells have changed. You seem to have (b) covered ok, not sure about (a)?
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
re PS (a) not covered - will tweak it

check you have sheets(2).select and not sheet2.select, please.
 
Upvote 0
Think so .... see bolded text in this extract from the end section (assume red items not what you're referring to):

ActiveWindow.ActivateNext
For myrow = 2 To 13
If Cells(myrow, 12) = 1 Then GoTo 100 Else GoTo 200
100 Rows(myrow).Select
Selection.Copy
Sheets(2).Select
Rows(Cells(1, 17)).Select
ActiveSheet.Paste
Sheets(1).Select
200 Next myrow
Sheets(1).Select
999
End Sub
 
Upvote 0
are your today and yesterday sheets the same number of rows as mine ? Do you have a second sheet in the today workbook - whatever that sheet is named ?
 
Upvote 0
Slightly different number of rows to yours ... hope this doesn't matter as when we finalise and put into use, the number of rows could be a few hundred and will vary from download to download. There are no extra sheets in either workbook.
 
Upvote 0
you need an extra sheet in the today workbook - you can have whatever number of rows you want - just need to amend loop limits - can be automated with a helper cell that counts non blank cells
 
Upvote 0
Sounds good .... if only I knew how:( Would I be better select the target cols (A to K), give them a range name ... in each workbook .... and then refer to that range name as reqd in the rest of the macro? Now that the macro does the highlighting job on the sample area used, I'm hoping with your expertise and patience to get it over the line. I'm happy to try anything you ask but am generally clueless on VBA stuff! I added a sheet to todaydata and the highlighting works but the new sheet aspect hasn't worked so far.

Q1 - does it matter if either workbook has filtering in use on one or more columns?
Q2 - does a header row matter?
 
Upvote 0
the macro wants to go to the second sheet in the today workbook to make a list of highlighted rows

at present do not have filtering

the macro allows for the first row to be the header row

no idea why it doesn't for you - because it does for me

it is a great pity I am not allowed to send you my spreadsheet...
 
Upvote 0
Ok Bob, we are so close as you can see from my test docs here! It seems to work fine up to the creation of a new sheet for the changed records/rows .... it is bombing now at this step "Rows(Cells(1, 17)).Select". It landsin cell A1 of Sheet1 in 'todaydata' but no no data added or any other sign of activity. I can possibly live with that if Col L has an entry of '1' for any changed record as appears to be what's happening; I could filter on these and copy/paste to new sheet. Can I please ask for your help with these 2 o/s points:

1) need to extend the target range beyond rows 1-13 as at present (I think) - ideally the full number of rows should be brought into play as the number is unknown into the future

2) need to highlight rows in 'todaydata' that don't have a match in 'yesterday' (they are likely to be new records added since the last download was run and will have a larger number (in the key field, Col-A)

If we get these resolved I think you can have a well-deserved break! Here's hoping! (time for bed now:rolleyes:)
 
Upvote 0
I think I have cracked it !!! In yesterday Z1 = automatic count of yesterday rows and BA1 is an automatic count of today rows. I have left the delay loop in so you can see the comparisons being made - I give the J and Z values for each cycle. If a today key is not found in yesterday, that today key is highlighted. Where keys match and one or more today cells are different, those cells are highlighted in both today and yesterday. After all this is done, the today sheet is updated so it shows the highlighted cells. Then finally, the rows in today that have one or more cells highlighted are copied to sheet 2 in the today workbook.

At the moment yesterday has 11 rows including the header, and today has 13. Ten of these are correctly copied to today, sheet 2.

Once you get this happening at your end, we can consider the number of columns varying, and columns in different order.


Sub Macro4()
'
' Macro4 Macro
' Macro recorded 22/11/2017 by bob
'


'
Range("A1:X1000").Select
Selection.Copy
ActiveWindow.ActivateNext
Range("AA1:Ax1000").Select
ActiveSheet.Paste
For j = 2 To Cells(1, 53)
For z = 2 To Cells(1, 26)
Cells(30, 9) = j
Cells(31, 9) = z
For delay = 1 To 100000000: Next delay
If Cells(z, 1) = Cells(j, 27) Then GoTo 20
15 Next z
Cells(j, 38) = 1
Cells(j, 27).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
GoTo 80
20 For y = 2 To 11
If Cells(z, y) = Cells(j, y + 26) Then GoTo 40 Else GoTo 30
30 Cells(z, y).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Cells(j, y + 26).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Cells(z, 12) = 1
Cells(j, 38) = 1
40 Next y
80 Next j
Range("AA1:AL13").Select
Selection.Copy
ActiveWindow.ActivateNext
Range("a1:l13").Select
ActiveSheet.Paste
ActiveWindow.ActivateNext
Range("AA1:AL13").Select
Selection.ClearContents
ActiveWindow.ActivateNext
For myrow = 2 To 13
If Cells(myrow, 12) = 1 Then GoTo 100 Else GoTo 200
100 Rows(myrow).Select
Selection.Copy
Sheets(2).Select
Rows(Cells(1, 17)).Select
ActiveSheet.Paste
Sheets(1).Select
200 Next myrow
Sheets(1).Select
999 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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