MS Excel Question

This should be quite easy - I just don’t know what the answer is. What I need to be able to do is:

I have two spreadsheets that should be exact replicas of each other. There are c. 1250 rows in each, and about 12 columns in each. Some cells in each row are WORDS, some are NUMBERS.

I can cut and paste one sheet underneath the other, and then sort them by whatever column I wish. Now the two lots of data are merged together - and if all is well, there should be two, and exactly two identical versions of each row.

What I need is a way to quickly compare all the cells in the 1st row to the 2nd row - but no further. Then all the cells in the 3rd Row to the 4th row, the 5th to the 6th… and so on.

Can someone help me?

I’m sure that it can be done.

Thanks

Do you have to copy one under the other?

My suggestion would be to copy and paste one of the spreadsheets to the right of the other. Then you would have one spreadsheet 1,250 rows by 24 columns. Then you could easily do =if(a1<>a13,“Oh why, God, WHY???”,“It’s all good”) or something along those lines in order to compare the values.

Why are you doing this? If you need two identical copies of the sheet, then:

note: the list needs column labels for this to work

copy one list below the other
select the entire list

For the menu:
Data
Advanced Filter

In the resulting po-up box:
Copy list to another location
Unique records only
Copy to some cell address not in the original list;

You should get a list of every individual row, including ones that are only in either one of the original lists.

Delete all the columns with the original duplicate list, and copy the sheet.

This gives you a single complete list with no duplicates.

If you need better instructions, post back.

Actually, it is a pop-up.

And copying the work-sheet will probably give you two complete lists with no duplicates.

Instead of combining your data in one worksheet to do your comparison, I suggest leaving the data in two sheets and adding a third. Make a copy of one of your sheets, and replace its data with formulas to compare the cells of the other two (using IF functions similar to Bean Counter’s suggestion).

Assuming your original worksheets are named “Sheet1” and “Sheet2”, the formula in cell A2 of the new sheet would be =IF(Sheet1!A2<>Sheet2!A2,“Not OK”,“OK”).

[aside]
I’ve been lurking for years and years (back to the Wally era). Thanks Achilles for posting the GQ that finally drew me out.
Rather anti-climactic, on preview.
[/aside]

This looks good - I’ll give it a shot after my morning meeting - YAAAWN… :wink:

This useful macro will do it:


Sub CompareWorksheetRanges(rng1 As Range, rng2 As Range)
Dim r As Long, c As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
If rng1 Is Nothing Or rng2 Is Nothing Then Exit Sub
If rng1.Areas.Count > 1 Or rng2.Areas.Count > 1 Then
MsgBox “Can’t compare multiple selections!”, _
vbExclamation, “Compare Worksheet Ranges”
Exit Sub
End If
Application.ScreenUpdating = False
Application.StatusBar = “Creating the report…”
Set rptWB = Workbooks.Add
Application.DisplayAlerts = False
While Worksheets.Count > 1
Worksheets(2).Delete
Wend
Application.DisplayAlerts = True
With rng1
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With rng2
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
If lr1 <> lr2 Or lc1 <> lc2 Then
If MsgBox(“The two ranges you want to compare are of different size!” & _
Chr(13) & “Do you want to continue anyway?”, _
vbQuestion + vbYesNo, “Compare Worksheet Ranges”) = vbNo Then Exit Sub
End If
DiffCount = 0
For c = 1 To maxC
Application.StatusBar = “Comparing cells " & _
Format(c / maxC, “0 %”) & “…”
For r = 1 To maxR
cf1 = “”
cf2 = “”
On Error Resume Next
cf1 = rng1.Cells(r, c).FormulaLocal
cf2 = rng2.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 <> cf2 Then
DiffCount = DiffCount + 1
Cells(r, c).Formula = “’” & cf1 & " <> " & cf2
End If
Next r
Next c
Application.StatusBar = “Formatting the report…”
With Range(Cells(1, 1), Cells(maxR, maxC))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns(“A:IV”).ColumnWidth = 20
rptWB.Saved = True
If DiffCount = 0 Then
rptWB.Close False
End If
Set rptWB = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox DiffCount & " cells contain different data!”, _
vbInformation, “Compare Worksheet Ranges”
End Sub

Sub TestCompareWorksheetRanges()

' compare two ranges in two different worksheets in two different workbooks
CompareWorksheetRanges ActiveWorkbook.Worksheets(1).Range("A1:O2000"), _
    Workbooks("OtherBook.xls").Worksheets(1).Range("A1:O2000")

End Sub

First change OtherBook (second last line) to one of the names of your two workbooks and then copy the whole thing. Go to your other workbook and hit [Alt] F11. Then select the Folder Microsoft Excel Objects and from the menu choose Insert>Module. Paste the macro in. Close it all up which will save it. If you like you can hit [Alt] F8 and select a shortcut key for the macro.

Now to run it just open both files go into the one containing the macro and run it. It will write a new workbook each time showing any differences between the files. If you go beyond 2000 rows adjust those numbers in the macro.

don’t ask:

In your macro, won’t missing a single row in one sheet cause ALL the following rows to be in the final report?

don’t ask - that is a great macro ! Thanks. by the way, you’re missing an “end if”, but appart from that - spot on !!!

What exactly does “wend” do - I cannot figure out what it means.

Thanks anyway !

Well of course if an extra blank row has been inserted. It is just checking each cell against the same address in the other workbook so inserting a bogus row will throw it off. If one row is blank but should have entries it will just report that row.

While Worksheets.Count > 1
Worksheets(2).Delete
Wend

“Wend” is the end of the While loop.

wend ends a while loop.

It isn’t actually my work I just have it in a library of useful odds and ends we hand around at work. It’s surprising how often someone at work says “If only you could…” and someone else will say “I have a macro that does that.”

Well thanks anyway. !!! :slight_smile:

You might want to mention that they are missing and “end if”…

Thanks again