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:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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
 
Upvote 0
rCount and cCount have same definition, whats the idea?
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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