PDA

View Full Version : Solved: Compare rows from two sheets and highlight non matched rows



sujittalukde
04-21-2008, 01:01 AM
I am using the following code to compare two sheets - Original with revised sheets and want to highlight the rows not matching.


Sub test()
Sheets("Original").Select
olrow = ActiveSheet.UsedRange.Rows.Count
Range("a1").Select
Sheets("revised").Select
rlrow = ActiveSheet.UsedRange.Rows.Count
Range("a1").Select
If olrow > rlrow Then
lastrow = olrow
Else
lastrow = rlow
End If
Sheets("Original").Select
Range("a1").Select
For i = 1 To lrow
Sheets("Original").Select
Range("a" & i).Select
Odt = ActiveCell.Offset(i, 0).Value
Oldgr = ActiveCell.Offset(i, 1).Value
Otot = ActiveCell.Offset(i, 8).Value
Sheets("revised").Select
Range("a" & i).Select
Rdt = ActiveCell.Offset(i, 0).Value
Rldgr = ActiveCell.Offset(i, 1).Value
Rtot = ActiveCell.Offset(i, 8).Value
If Odt & Oldgr & Otot <> Rdt & Rldgr & Rtot Then
ActiveCell.Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Range("A" & i).Select
End If
Next i
End Sub

However the code is not entering into For loop. Can someone please help me in sorting this out?
A sample copy is attached for ready reference.

tstav
04-21-2008, 01:27 AM
Variable misspelled:
lastrow = rlow <--- rlrow

Use Option Explicit before all code, declare all variables and you'll never face such errors, ever!

sujittalukde
04-21-2008, 02:39 AM
Thanks for the obsevation. Now I have rectified the code and working great. fianl code is given:


Option Explicit
Sub test()
Dim olrow As Double
Dim rlrow As Double
Dim lastrow As Double
Dim i, j As Double
Dim Odt As Double
Dim Oldgr As String
Dim Otot As Double
Dim Rdt As Double
Dim Rldgr As String
Dim Rtot As Double
Sheets("Original").Select
olrow = ActiveSheet.UsedRange.Rows.Count
Range("a1").Select
Sheets("revised").Select
Range("a2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Interior.ColorIndex = xlNone
rlrow = ActiveSheet.UsedRange.Rows.Count
Range("a1").Select
If olrow > rlrow Then
lastrow = olrow
Else
lastrow = rlrow
End If
Sheets("Original").Select
Range("a1").Select
j = 0
For i = 1 To lastrow
j = j + 1
Sheets("Original").Select
Range("a1").Select
Odt = ActiveCell.Offset(j, 0).Value
Oldgr = ActiveCell.Offset(j, 1).Value
Otot = ActiveCell.Offset(j, 8).Value
Sheets("revised").Select
Range("A1").Select

Rdt = ActiveCell.Offset(j, 0).Value
Rldgr = ActiveCell.Offset(j, 1).Value
Rtot = ActiveCell.Offset(j, 8).Value
If Odt & Oldgr & Otot <> Rdt & Rldgr & Rtot Then
ActiveCell.Offset(j, 0).Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Range("a1").Select
End If
Next i
End Sub