PC Review


Reply
Thread Tools Rate Thread

Compating all rows in two sheets

 
 
Utkarsh
Guest
Posts: n/a
 
      12th Dec 2010
I have two sheets with similar data

Sheet1
Name Date Type Amount
a 01-01-2010 x 100
b 01-01-2007 y 25
a 01-03-2008 x 124

Sheet2
Name Date Type Amount
a 01-01-2010 x 100
b 01-01-2007 y 20
a 01-03-2008 x 124
d 01-05-2007 x 100

I am using the below code to flag enties that are not exactly
matching. Thus it should flag for me that Name"b" and "d" are not
found in Sheet1. It works propoerly for "d" but not for "b". Obviously
I am amking a mistake. Please advise.

Sub compare()
'clear columne
Sheets("Sheet2").Range("E:E").ClearContents

'Find the last row to be evaluated for each sheet
Sheets("Sheet1").Select
lastrow1 = Range("A65536").End(xlUp).Row
Sheets("Sheet2").Select
lastrow2 = Range("A65536").End(xlUp).Row

Sheets("Sheet1").Select
For i = 2 To lastrow1
'Transfer each foled for a row into a variable
rec1 = Cells(i, 1).Value
rec2 = Cells(i, 2).Value
rec3 = Cells(i, 3).Value
rec4 = Cells(i, 4).Value


Sheets("Sheet2").Select
For j = 2 To lastrow2

'Look for a match
If Cells(i, 1).Value = rec1 And Cells(i, 2).Value = rec2 And _
Cells(i, 3).Value = rec3 And _
Cells(i, 4).Value = rec4 Then

Cells(i, 5).Value = "Found"

End If

Next j
Next i

End Sub
 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      12th Dec 2010
The first thing that sticks out is the selection of Sheet1 is done outside of both loops.
So most of your code it is comparing sheet2 to sheet2.
You could place the selection inside the loop and see what happens.
I have not tried to run your code...
'---
Sheets("Sheet1").Select
For i = 2 To lastrow1

-should be-

For i = 2 To lastrow1
Sheets("Sheet1").Select
---
Jim Cone
Portland, Oregon USA
http://tinyurl.com/XLCompanion
(compare stuff)
..
..
..

"Utkarsh" <(E-Mail Removed)>
wrote in message
news:12d99e91-4b12-4f1b-9a41-(E-Mail Removed)...
I have two sheets with similar data

Sheet1
Name Date Type Amount
a 01-01-2010 x 100
b 01-01-2007 y 25
a 01-03-2008 x 124

Sheet2
Name Date Type Amount
a 01-01-2010 x 100
b 01-01-2007 y 20
a 01-03-2008 x 124
d 01-05-2007 x 100

I am using the below code to flag enties that are not exactly
matching. Thus it should flag for me that Name"b" and "d" are not
found in Sheet1. It works propoerly for "d" but not for "b". Obviously
I am amking a mistake. Please advise.

Sub compare()
'clear columne
Sheets("Sheet2").Range("E:E").ClearContents

'Find the last row to be evaluated for each sheet
Sheets("Sheet1").Select
lastrow1 = Range("A65536").End(xlUp).Row
Sheets("Sheet2").Select
lastrow2 = Range("A65536").End(xlUp).Row

Sheets("Sheet1").Select
For i = 2 To lastrow1
'Transfer each foled for a row into a variable
rec1 = Cells(i, 1).Value
rec2 = Cells(i, 2).Value
rec3 = Cells(i, 3).Value
rec4 = Cells(i, 4).Value


Sheets("Sheet2").Select
For j = 2 To lastrow2

'Look for a match
If Cells(i, 1).Value = rec1 And Cells(i, 2).Value = rec2 And _
Cells(i, 3).Value = rec3 And _
Cells(i, 4).Value = rec4 Then

Cells(i, 5).Value = "Found"
End If
Next j
Next i
End Sub
 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      12th Dec 2010
You also need to replace the "i" values with "j" in this part of the second loop....
'--
If Cells(i, 1).Value = rec1 And Cells(i, 2).Value = rec2 And _
Cells(i, 3).Value = rec3 And _
Cells(i, 4).Value = rec4 Then

Cells(i, 5).Value = "Found"
End If
'--
Jim Cone
Portland, Oregon USA
Sub
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MACRO THAT CUT ROWS BETWEEN SHEETS AND ADD EMPTY ROWS udi Hen Microsoft Excel Programming 0 18th Dec 2008 02:41 PM
"Add/Remove Rows Code" adds rows on grouped sheets, but won't remove rows. Conan Kelly Microsoft Excel Programming 1 16th Nov 2007 10:41 PM
Matching rows in 2 sheets and copying matching rows from sheet 1 t =?Utf-8?B?ZmJhZ2lyb3Y=?= Microsoft Excel Programming 1 8th Apr 2007 03:44 PM
"With Sheets" Issue - macro on one sheet to affect hidden rows on other sheets Punsterr Microsoft Excel Programming 3 21st Feb 2006 04:01 AM
linking rows on sheets without empty rows in between Frank L Microsoft Excel Misc 0 17th Aug 2004 06:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:06 PM.