Comparing two excel sheets, same workbook, highlight differences

Doozae

New Member
Joined
Oct 1, 2014
Messages
8
I am doing my first steps in excel 2007 vba. What I am trying to do is to code a macro that compares my last and my second last excel sheet and highlights the differences in the last one. As you can see below I coded the following and Excel does not display any errors when I use the debug mode, yet it does not mark any cells where a change has been made, which it is suppose to do. Would anyone mind having a look and correct my potential mistakes in the code below. Thank you very much.

<tbody>
</tbody>

Code:
<code>Sub compare2sheets() 
Dim sh1 As Worksheet, sh2 As Worksheet 
Dim rCount As Long, cCount As Long 
Set sh1 = Worksheets(Sheets.Count - 1) 
Set sh2 = Worksheets(Sheets.Count) 
rCount = sh1.Cells(Rows.Count, 1).End(xlUp).Row 
cCount = sh1.Cells(Rows.Count, 1).End(xlUp).Row 
Dim r As Long, c As Integer For r = 1 To rCount    
 For c = 1 To cCount        
 If sh1.Cells(r, c) <> sh2.Cells(r, c) Then            
sh2.Cells(r, c).Interior.ColorIndex = 3         
End If     
Next c 
Next r</code>
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Ragnar1211

Well-known Member
Joined
Jul 10, 2008
Messages
571
Rather than VBA, you use Conditional formatting, much faster.

But, your code works for me in a simple workbook with only 2 sheets.
Perhaps type in your sheet names?
Code:
Sub compare2sheets()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim rCount As Long, cCount As Long
[COLOR=#ff0000]Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")[/COLOR]
rCount = sh1.Cells(Rows.Count, 1).End(xlUp).Row
cCount = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Dim r As Long, c As Integer

For r = 1 To rCount
    For c = 1 To cCount
        If sh1.Cells(r, c) <> sh2.Cells(r, c) Then
        sh2.Cells(r, c).Interior.ColorIndex = 3
        End If
    Next c
Next r

End Sub
 

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
4,040
Office Version
  1. 365
Platform
  1. Windows
rCount and cCount have same definition, whats the idea?
 

Watch MrExcel Video

Forum statistics

Threads
1,109,364
Messages
5,528,265
Members
409,811
Latest member
pjwhyman

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top