comparing 2 worksheets in a workbook

LFKim2018

Active Member
Joined
Mar 24, 2018
Messages
267
In the same workbook: I have one worksheet named pc1 , another named pc
I want to compare both - if identical (exactly the same) - then exit the sub routine
the syntax below is not working - pls help in the correction (I have search the net but couldn't find the right one)

inside a sub routine
If Worksheets("pc1") = Worksheets("pc") Then
MsgBox "No changes found..the 2 worksheets are identical"
exit sub
End If
End Sub

the [Worksheets("pc1") = Worksheets("pc")] is hi-lighted to be incorrect - how should this be written?

many thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this code

Code:
Sub CompareSheets()
Dim cel As Range


For Each cel In Sheets("pc").UsedRange


If cel.Value <> Sheets("pc1").Cells(cel.Row, cel.Column).Value Then
MsgBox ("Unidentival cell found " & cel.Address & " in Sheet pc")
Exit Sub
End If


Next cel


MsgBox ("Sheets found identical")


End Sub
 
Last edited:
Upvote 0
Another option
Code:
Sub chk()
Dim ary1 As Variant
Dim ary2 As Variant
Dim i As Long, j As Long

ary1 = Sheets("Postcodes").UsedRange
ary2 = Sheets("Pcode").UsedRange
For i = 1 To UBound(ary1)
   For j = 1 To UBound(ary1, 2)
      If Not ary1(i, j) = ary2(i, j) Then
         MsgBox " Not the same"
         Exit Sub
      End If
   Next j
Next i
MsgBox "The same"
End Sub
 
Upvote 0
try this code

Code:
sub comparesheets()
dim cel as range


for each cel in sheets("pc").usedrange


if cel.value <> sheets("pc1").cells(cel.row, cel.column).value then
msgbox ("unidentival cell found " & cel.address & " in sheet pc")
exit sub
end if


next cel


msgbox ("sheets found identical")


end sub

thank you very much!
It worked nicely..
 
Upvote 0
another option
Code:
sub chk()
dim ary1 as variant
dim ary2 as variant
dim i as long, j as long

ary1 = sheets("postcodes").usedrange
ary2 = sheets("pcode").usedrange
for i = 1 to ubound(ary1)
   for j = 1 to ubound(ary1, 2)
      if not ary1(i, j) = ary2(i, j) then
         msgbox " not the same"
         exit sub
      end if
   next j
next i
msgbox "the same"
end sub

thank you for your reply
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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