Compare Rows via VBA Loop

wildcatcrazy

New Member
Joined
Sep 20, 2011
Messages
12
I have a simple macro that I recorded and then edited in VB. But, I would like to turn this into a loop so I do not have to make over a 1000 entries just to compare the remaining rows. What I need it to do is compare rows 2:3, 4:5, 6:7, so and so on to highlight the differences in the columns until it reaches the end of the data. When it does the compare, it needs to highlight the changes and move on to the next set of rows. I was able to duplicate these entries to compare all of the remaining rows (two at a time) until I got to around 500. Then Excel said I exceeded the limit.

Here is the macro I started with:

Sub Compare()
'
' Compare Macro
'
' Keyboard Shortcut: Ctrl+l
'

Rows("2:3").Select
Selection.ColumnDifferences(ActiveCell).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Something like this will compare in pairs from row 2 to the last row

The code
- uses error handling in case there are no differences between the paired rows
- collates any differences into a range (rng3)
- formats rng3 at the end if it exists

If you are doing lots of spreadsheet auditing you may find my Mappit! addin useful, see http://www.experts-exchange.com/A_2613.html



Code:
Sub PairedRows()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range

    Dim lngRow As Long
    Set rng1 = ActiveSheet.Cells.Find("*", [a1], xlValues, , xlByRows, xlPrevious)
    On Error Resume Next
    For lngRow = 2 To rng1.Row Step 2
        Set rng2 = Nothing
        Set rng2 = Rows(lngRow & ":" & lngRow + 1).ColumnDifferences(Rows(lngRow).Cells(1))
        If Not rng2 Is Nothing Then
            If Not rng3 Is Nothing Then
                Set rng3 = Union(rng2, rng3)
            Else
                Set rng3 = rng2
            End If
        End If
    Next
    On Error GoTo 0
    If Not rng3 Is Nothing Then rng3.Interior.Color = 65535
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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