Compare two worksheets and highlight changed cells on one worksheet

Phil Payne

Board Regular
Joined
May 17, 2013
Messages
131
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have two worksheets in the same workbook. The first worksheet contains last periods score matrix, the second worksheet contains this periods score matrix.

I am struggling to find a away to highlight those cells in this periods worksheet that are different from last periods worksheet.

I have been able to go as far as identifying the changed cells. I know that works from use of 'MsgBoxes', however I cannot find a way to highlight the identified cells. Its probably because I have chosen to go about this the wrong way entirely. Can someone please give me a guide as to how I should go about this?

The code I have that works (according to MsgBox's anyway) is below. I would very mush appreciate any guidance whatsoever.
Thanks,

Code:
Option Explicit

Sub B_HighlightDifferences()
'Workbooks("Scoring Matrix NEW").Activate
    Dim varScoring As Variant
    Dim varScoring_OLD As Variant
    Dim strRangeToCheck As String
    Dim irow As Long
    Dim icol As Long
    Dim color As CellFormat
    
    strRangeToCheck = "bl9:bo15"  'smallrange for testing purposes only
 
    varScoring = Worksheets("Scoring").Range(strRangeToCheck)
    varScoring_OLD = Worksheets("Scoring_OLD").Range(strRangeToCheck)
 
    For irow = LBound(varScoring, 1) To UBound(varScoring, 1)
        For icol = LBound(varScoring, 2) To UBound(varScoring, 2)

            If varScoring(irow, icol) = varScoring_OLD(irow, icol) Then
         [I]       ' Cells are identical. ' Do nothing.[/I]
                MsgBox "This has not changed"
            Else
               [I] ' Cells are different. [/I]
		[B]' Need code here to highlight each cell that is different[/B]
                 MsgBox "This has changed"


End If
            End If
        Next icol
    Next irow

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Option Explicit


Sub B_HighlightDifferences()
    'Workbooks("Scoring Matrix NEW").Activate
    Dim rScoring As Range
    Dim rScoring_OLD As Range
    Dim strRangeToCheck As String
    Dim irow As Long
    Dim icol As Long
    Dim color As CellFormat
    Dim rCell As Range
    
    strRangeToCheck = "bl9:bo15"  'smallrange for testing purposes only
    
    rScoring = Worksheets("Scoring").Range(strRangeToCheck)
    rScoring_OLD = Worksheets("Scoring_OLD").Range(strRangeToCheck)
    
    For irow = 1 To rScoring.Rows.Count
        For icol = 1 To rScoring.Columns.Count
            
            If rScoring(irow, icol) = rScoring_OLD(irow, icol) Then
                'do nothing
            Else
                ' Cells are different.
                ' Need code here to highlight each cell that is different
                Set rCell = rScoring(irow, icol)
                rCell.Interior.color = vbRed
                
            End If
        End If
    Next icol
Next irow


End Sub

This should work. I've changed your variants to ranges for ease of coding (it enables intellisense).
 
Upvote 0
Hello BrianMH,

Thanks very much for your prompt reply.

I had an 'endIf without block if' msg so I removed the last endif, but on the next run I got a runtime error (91) "Object variable or With block variable not set" on the code line -
" rScoring = Worksheets("Scoring").Range(strRangeToCheck)"

Any ideas?

Code:
Option Explicit


Sub B_HighlightDifferences()
    'Workbooks("Scoring Matrix NEW").Activate
    Dim rScoring As Range
    Dim rScoring_OLD As Range
    Dim strRangeToCheck As String
    Dim irow As Long
    Dim icol As Long
    Dim color As CellFormat
    Dim rCell As Range
    
    strRangeToCheck = "bl9:bo15"  'smallrange for testing purposes only
    
    rScoring = Worksheets("Scoring").Range(strRangeToCheck)
    rScoring_OLD = Worksheets("Scoring_OLD").Range(strRangeToCheck)
    
    For irow = 1 To rScoring.Rows.Count
        For icol = 1 To rScoring.Columns.Count
            
            If rScoring(irow, icol) = rScoring_OLD(irow, icol) Then
                'do nothing
            Else
                ' Cells are different.
                ' Need code here to highlight each cell that is different
                Set rCell = rScoring(irow, icol)
                rCell.Interior.color = vbRed
                
            End If
        End If
    Next icol
Next irow


End Sub

This should work. I've changed your variants to ranges for ease of coding (it enables intellisense).
 
Upvote 0
Solution found - see below


Thanks all.


Code:
     If varScoring(irow, icol) = varScoring_OLD(irow, icol) Then
                ' Cells are identical. ' Do nothing.
            Else
                ' Cells are different.
[I][B]                Worksheets("Scoring").Range(strRangeToCheck).Cells(irow, icol).Interior.Color = vbRed[/B][/I]
            End If
 
Upvote 0

Forum statistics

Threads
1,217,307
Messages
6,135,749
Members
449,963
Latest member
palm

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