PC Review


Reply
Thread Tools Rate Thread

Compare Rows on different Worksheets and Output Difference's to other Worksheets.

 
 
dids72@gmail.com
Guest
Posts: n/a
 
      19th Sep 2007
Hello,

I am trying to figure out how to compare the data from rows across
multiple columns on different worksheets. What I am trying to do is
output the data that is in Worksheet 1 and not in Worksheet 2 into
Worksheet 3. Then in addition also output the data that appears in
Worksheet 2 but not in Worksheet 1 into Worksheet 4. One key aspect
would be if the same data appears multiple times' for example it may
appear 2 times on Worksheet 1 but only 1 time in Worksheet 2. The
second appearance of the data would be copied to Worksheet 3.

For Example:

Worksheet
1 Worksheet 2

Column A Column B Column
A Column B

080107 AN3205
080107 AN3205
080207 AN3205
080107 AN3500
080107 AN3500
080207 AN3501
080107 AN3501
080207 AN3501
080107 AN3510
080107 AN3510
080107 AN3510


When the Compare macro is run it would produce the following output.
Note that data entry 080107 AN3510 appears on Worksheet 3 because it
appears 2 time's on Worksheet 1 but only 1 time on Worksheet 2.
Also, note that data entry 080207 AN3501 appears 2 times on Worksheet
4 because it appears 2 times on Worksheet 2 and not at all on
Worksheet 1.


Worksheet
3 Worksheet 4

Column A Column B Column
A Column B

080207 AN3205
080207 AN3501
080107 AN3510
080207 AN3501








I am trying to modify the code below, that Tom Ogilvy posted in the
past, to address my needs. I have added questions to the code to see
if someone could help me understand it. If someone would help me to
understand or given me a little direction on how to proceed. It would
be greatly appreciated.

Sub ProcessData()
Dim rng1 As Range, rng2 As Range
Dim cell As Range, rw As Long
Dim cnt As Long, c As Range
Dim firstAddress As String
Dim i As Long, bFound As Boolean


'Does "Range(.Cells(2, 7)" indicate Columns 2 and 7 or 2 through 7?
'Is "Cells(Rows.Count, 7).End(xlUp))" counting the number of rows in
Column 7?

With Worksheets("Sheet1")
Set rng1 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
End With
With Worksheets("Sheet2")
Set rng2 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
End With
rw = rng2.Rows(rng2.Rows.Count).Row + 1
For Each cell In rng1
Set c = rng2.Find(cell, LookIn:=xlValues)


'What is this part of the code doing?

If Not c Is Nothing Then
firstAddress = c.Address
bFound = False
Do
cnt = 0
For i = -2 To -6 Step -1
If cell.Offset(0, i) <> c.Offset(0, i) Then
Exit For
End If
cnt = cnt + 1
Next i
If cnt = 5 Then
bFound = True
Exit Do
End If
Set c = rng2.FindNext(c)
Loop While c.Address <> firstAddress
If bFound = False Then
cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
rw = rw + 1
End If
Else
cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
rw = rw + 1
End If
Next cell
End Sub


Again any help you would be able to provide would be greatly
appreciated. I don't know to much about VBA and am trying to learn to
get this done for a friend of mine.

Thanks,

Rich

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      19th Sep 2007
If you want to send a sample/abbreviated file to (E-Mail Removed) with your
source worksheets and a worksheet showing expected output annotated as to why
each item was placed there (in the output sheet - that covers all
contingencies), then I can provide you a macro that performs the action.

The wordwrap in the posting makes it difficult to decipher

--
regards,
Tom Ogilvy


"(E-Mail Removed)" wrote:

> Hello,
>
> I am trying to figure out how to compare the data from rows across
> multiple columns on different worksheets. What I am trying to do is
> output the data that is in Worksheet 1 and not in Worksheet 2 into
> Worksheet 3. Then in addition also output the data that appears in
> Worksheet 2 but not in Worksheet 1 into Worksheet 4. One key aspect
> would be if the same data appears multiple times' for example it may
> appear 2 times on Worksheet 1 but only 1 time in Worksheet 2. The
> second appearance of the data would be copied to Worksheet 3.
>
> For Example:
>
> Worksheet
> 1 Worksheet 2
>
> Column A Column B Column
> A Column B
>
> 080107 AN3205
> 080107 AN3205
> 080207 AN3205
> 080107 AN3500
> 080107 AN3500
> 080207 AN3501
> 080107 AN3501
> 080207 AN3501
> 080107 AN3510
> 080107 AN3510
> 080107 AN3510
>
>
> When the Compare macro is run it would produce the following output.
> Note that data entry 080107 AN3510 appears on Worksheet 3 because it
> appears 2 time's on Worksheet 1 but only 1 time on Worksheet 2.
> Also, note that data entry 080207 AN3501 appears 2 times on Worksheet
> 4 because it appears 2 times on Worksheet 2 and not at all on
> Worksheet 1.
>
>
> Worksheet
> 3 Worksheet 4
>
> Column A Column B Column
> A Column B
>
> 080207 AN3205
> 080207 AN3501
> 080107 AN3510
> 080207 AN3501
>
>
>
>
>
>
>
>
> I am trying to modify the code below, that Tom Ogilvy posted in the
> past, to address my needs. I have added questions to the code to see
> if someone could help me understand it. If someone would help me to
> understand or given me a little direction on how to proceed. It would
> be greatly appreciated.
>
> Sub ProcessData()
> Dim rng1 As Range, rng2 As Range
> Dim cell As Range, rw As Long
> Dim cnt As Long, c As Range
> Dim firstAddress As String
> Dim i As Long, bFound As Boolean
>
>
> 'Does "Range(.Cells(2, 7)" indicate Columns 2 and 7 or 2 through 7?
> 'Is "Cells(Rows.Count, 7).End(xlUp))" counting the number of rows in
> Column 7?
>
> With Worksheets("Sheet1")
> Set rng1 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
> End With
> With Worksheets("Sheet2")
> Set rng2 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
> End With
> rw = rng2.Rows(rng2.Rows.Count).Row + 1
> For Each cell In rng1
> Set c = rng2.Find(cell, LookIn:=xlValues)
>
>
> 'What is this part of the code doing?
>
> If Not c Is Nothing Then
> firstAddress = c.Address
> bFound = False
> Do
> cnt = 0
> For i = -2 To -6 Step -1
> If cell.Offset(0, i) <> c.Offset(0, i) Then
> Exit For
> End If
> cnt = cnt + 1
> Next i
> If cnt = 5 Then
> bFound = True
> Exit Do
> End If
> Set c = rng2.FindNext(c)
> Loop While c.Address <> firstAddress
> If bFound = False Then
> cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
> rw = rw + 1
> End If
> Else
> cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
> rw = rw + 1
> End If
> Next cell
> End Sub
>
>
> Again any help you would be able to provide would be greatly
> appreciated. I don't know to much about VBA and am trying to learn to
> get this done for a friend of mine.
>
> Thanks,
>
> Rich
>
>

 
Reply With Quote
 
dids72@gmail.com
Guest
Posts: n/a
 
      19th Sep 2007
On Sep 19, 9:22 am, Tom Ogilvy <TomOgi...@discussions.microsoft.com>
wrote:
> If you want to send a sample/abbreviated file to twogi...@msn.com with your
> source worksheets and a worksheet showing expected output annotated as to why
> each item was placed there (in the output sheet - that covers all
> contingencies), then I can provide you a macro that performs the action.
>
> The wordwrap in the posting makes it difficult to decipher
>
> --
> regards,
> Tom Ogilvy
>
>
>
> "did...@gmail.com" wrote:
> > Hello,

>
> > I am trying to figure out how to compare the data from rows across
> > multiple columns on different worksheets. What I am trying to do is
> > output the data that is in Worksheet 1 and not in Worksheet 2 into
> > Worksheet 3. Then in addition also output the data that appears in
> > Worksheet 2 but not in Worksheet 1 into Worksheet 4. One key aspect
> > would be if the same data appears multiple times' for example it may
> > appear 2 times on Worksheet 1 but only 1 time in Worksheet 2. The
> > second appearance of the data would be copied to Worksheet 3.

>
> > For Example:

>
> > Worksheet
> > 1 Worksheet 2

>
> > Column A Column B Column
> > A Column B

>
> > 080107 AN3205
> > 080107 AN3205
> > 080207 AN3205
> > 080107 AN3500
> > 080107 AN3500
> > 080207 AN3501
> > 080107 AN3501
> > 080207 AN3501
> > 080107 AN3510
> > 080107 AN3510
> > 080107 AN3510

>
> > When the Compare macro is run it would produce the following output.
> > Note that data entry 080107 AN3510 appears on Worksheet 3 because it
> > appears 2 time's on Worksheet 1 but only 1 time on Worksheet 2.
> > Also, note that data entry 080207 AN3501 appears 2 times on Worksheet
> > 4 because it appears 2 times on Worksheet 2 and not at all on
> > Worksheet 1.

>
> > Worksheet
> > 3 Worksheet 4

>
> > Column A Column B Column
> > A Column B

>
> > 080207 AN3205
> > 080207 AN3501
> > 080107 AN3510
> > 080207 AN3501

>
> > I am trying to modify the code below, that Tom Ogilvy posted in the
> > past, to address my needs. I have added questions to the code to see
> > if someone could help me understand it. If someone would help me to
> > understand or given me a little direction on how to proceed. It would
> > be greatly appreciated.

>
> > Sub ProcessData()
> > Dim rng1 As Range, rng2 As Range
> > Dim cell As Range, rw As Long
> > Dim cnt As Long, c As Range
> > Dim firstAddress As String
> > Dim i As Long, bFound As Boolean

>
> > 'Does "Range(.Cells(2, 7)" indicate Columns 2 and 7 or 2 through 7?
> > 'Is "Cells(Rows.Count, 7).End(xlUp))" counting the number of rows in
> > Column 7?

>
> > With Worksheets("Sheet1")
> > Set rng1 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
> > End With
> > With Worksheets("Sheet2")
> > Set rng2 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
> > End With
> > rw = rng2.Rows(rng2.Rows.Count).Row + 1
> > For Each cell In rng1
> > Set c = rng2.Find(cell, LookIn:=xlValues)

>
> > 'What is this part of the code doing?

>
> > If Not c Is Nothing Then
> > firstAddress = c.Address
> > bFound = False
> > Do
> > cnt = 0
> > For i = -2 To -6 Step -1
> > If cell.Offset(0, i) <> c.Offset(0, i) Then
> > Exit For
> > End If
> > cnt = cnt + 1
> > Next i
> > If cnt = 5 Then
> > bFound = True
> > Exit Do
> > End If
> > Set c = rng2.FindNext(c)
> > Loop While c.Address <> firstAddress
> > If bFound = False Then
> > cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
> > rw = rw + 1
> > End If
> > Else
> > cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
> > rw = rw + 1
> > End If
> > Next cell
> > End Sub

>
> > Again any help you would be able to provide would be greatly
> > appreciated. I don't know to much about VBA and am trying to learn to
> > get this done for a friend of mine.

>
> > Thanks,

>
> > Rich- Hide quoted text -

>
> - Show quoted text -


Hi Tom,

I just sent you a sample file. Thank you very much for taking the
time to help me.

Thanks,

Rich

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      19th Sep 2007
Sent you are response.

--
Regards,
Tom Ogilvy



"(E-Mail Removed)" wrote:

> On Sep 19, 9:22 am, Tom Ogilvy <TomOgi...@discussions.microsoft.com>
> wrote:
> > If you want to send a sample/abbreviated file to twogi...@msn.com with your
> > source worksheets and a worksheet showing expected output annotated as to why
> > each item was placed there (in the output sheet - that covers all
> > contingencies), then I can provide you a macro that performs the action.
> >
> > The wordwrap in the posting makes it difficult to decipher
> >
> > --
> > regards,
> > Tom Ogilvy
> >
> >
> >
> > "did...@gmail.com" wrote:
> > > Hello,

> >
> > > I am trying to figure out how to compare the data from rows across
> > > multiple columns on different worksheets. What I am trying to do is
> > > output the data that is in Worksheet 1 and not in Worksheet 2 into
> > > Worksheet 3. Then in addition also output the data that appears in
> > > Worksheet 2 but not in Worksheet 1 into Worksheet 4. One key aspect
> > > would be if the same data appears multiple times' for example it may
> > > appear 2 times on Worksheet 1 but only 1 time in Worksheet 2. The
> > > second appearance of the data would be copied to Worksheet 3.

> >
> > > For Example:

> >
> > > Worksheet
> > > 1 Worksheet 2

> >
> > > Column A Column B Column
> > > A Column B

> >
> > > 080107 AN3205
> > > 080107 AN3205
> > > 080207 AN3205
> > > 080107 AN3500
> > > 080107 AN3500
> > > 080207 AN3501
> > > 080107 AN3501
> > > 080207 AN3501
> > > 080107 AN3510
> > > 080107 AN3510
> > > 080107 AN3510

> >
> > > When the Compare macro is run it would produce the following output.
> > > Note that data entry 080107 AN3510 appears on Worksheet 3 because it
> > > appears 2 time's on Worksheet 1 but only 1 time on Worksheet 2.
> > > Also, note that data entry 080207 AN3501 appears 2 times on Worksheet
> > > 4 because it appears 2 times on Worksheet 2 and not at all on
> > > Worksheet 1.

> >
> > > Worksheet
> > > 3 Worksheet 4

> >
> > > Column A Column B Column
> > > A Column B

> >
> > > 080207 AN3205
> > > 080207 AN3501
> > > 080107 AN3510
> > > 080207 AN3501

> >
> > > I am trying to modify the code below, that Tom Ogilvy posted in the
> > > past, to address my needs. I have added questions to the code to see
> > > if someone could help me understand it. If someone would help me to
> > > understand or given me a little direction on how to proceed. It would
> > > be greatly appreciated.

> >
> > > Sub ProcessData()
> > > Dim rng1 As Range, rng2 As Range
> > > Dim cell As Range, rw As Long
> > > Dim cnt As Long, c As Range
> > > Dim firstAddress As String
> > > Dim i As Long, bFound As Boolean

> >
> > > 'Does "Range(.Cells(2, 7)" indicate Columns 2 and 7 or 2 through 7?
> > > 'Is "Cells(Rows.Count, 7).End(xlUp))" counting the number of rows in
> > > Column 7?

> >
> > > With Worksheets("Sheet1")
> > > Set rng1 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
> > > End With
> > > With Worksheets("Sheet2")
> > > Set rng2 = .Range(.Cells(2, 7), .Cells(Rows.Count, 7).End(xlUp))
> > > End With
> > > rw = rng2.Rows(rng2.Rows.Count).Row + 1
> > > For Each cell In rng1
> > > Set c = rng2.Find(cell, LookIn:=xlValues)

> >
> > > 'What is this part of the code doing?

> >
> > > If Not c Is Nothing Then
> > > firstAddress = c.Address
> > > bFound = False
> > > Do
> > > cnt = 0
> > > For i = -2 To -6 Step -1
> > > If cell.Offset(0, i) <> c.Offset(0, i) Then
> > > Exit For
> > > End If
> > > cnt = cnt + 1
> > > Next i
> > > If cnt = 5 Then
> > > bFound = True
> > > Exit Do
> > > End If
> > > Set c = rng2.FindNext(c)
> > > Loop While c.Address <> firstAddress
> > > If bFound = False Then
> > > cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
> > > rw = rw + 1
> > > End If
> > > Else
> > > cell.EntireRow.Copy Worksheets("sheet2").Cells(rw, 1)
> > > rw = rw + 1
> > > End If
> > > Next cell
> > > End Sub

> >
> > > Again any help you would be able to provide would be greatly
> > > appreciated. I don't know to much about VBA and am trying to learn to
> > > get this done for a friend of mine.

> >
> > > Thanks,

> >
> > > Rich- Hide quoted text -

> >
> > - Show quoted text -

>
> Hi Tom,
>
> I just sent you a sample file. Thank you very much for taking the
> time to help me.
>
> Thanks,
>
> Rich
>
>

 
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
Compare two worksheets in the same workbook and highlight difference mrcreid Microsoft Excel Programming 7 10th Aug 2010 08:31 PM
Compare difference between 2 worksheets 2003 Annie Microsoft Excel Misc 1 27th Apr 2010 06:12 PM
Compare data from two worksheets and display difference into 3rd s auntiespyke Microsoft Excel Worksheet Functions 4 6th Aug 2008 07:02 PM
Compare two worksheets and highlight the one sheet’s difference from the other one minrufeng Microsoft Excel Programming 1 24th Feb 2006 10:19 PM
How do I compare 2 worksheets, 1 old, 1 updated to find difference =?Utf-8?B?YWxpZW5zdGV3?= Microsoft Excel Misc 1 31st Jan 2005 02:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:38 PM.