PC Review


Reply
Thread Tools Rate Thread

Compare two worksheets and Highlight duplicates in each

 
 
=?Utf-8?B?TGlzYWI=?=
Guest
Posts: n/a
 
      11th Sep 2007
PLEASE HELP!
I am trying to highlight every row in a worksheet that has a duplicate in
another worksheet in the same excel file.

I have never programmed in excel but I am very skilled at VBA.

I am using the following code. However, it errors out in the IF THEN
statement.

***Unable to set the Pattern property of the interior class**

I also tried using the ColorIndex property and I get the same error-
Selection.Rows(counter).Interior.ColorIndex = vbYellow
---------------
Sub Find_MatchesINZips()

Dim compareRange As Variant
Dim x As Variant, y As Variant
Dim counter As Integer

Set compareRange = Worksheets(2).Range("A2:A149")
counter = 1

For Each x In Selection
For Each y In compareRange
If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow
Next y
counter = counter + 1
Next x

End Sub
 
Reply With Quote
 
 
 
 
JW
Guest
Posts: n/a
 
      11th Sep 2007
If x = y Then Selection.Rows(counter).EntireRow.Interior.ColorIndex =
6

Lisab wrote:
> PLEASE HELP!
> I am trying to highlight every row in a worksheet that has a duplicate in
> another worksheet in the same excel file.
>
> I have never programmed in excel but I am very skilled at VBA.
>
> I am using the following code. However, it errors out in the IF THEN
> statement.
>
> ***Unable to set the Pattern property of the interior class**
>
> I also tried using the ColorIndex property and I get the same error-
> Selection.Rows(counter).Interior.ColorIndex = vbYellow
> ---------------
> Sub Find_MatchesINZips()
>
> Dim compareRange As Variant
> Dim x As Variant, y As Variant
> Dim counter As Integer
>
> Set compareRange = Worksheets(2).Range("A2:A149")
> counter = 1
>
> For Each x In Selection
> For Each y In compareRange
> If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow
> Next y
> counter = counter + 1
> Next x
>
> End Sub


 
Reply With Quote
 
=?Utf-8?B?TGlzYWI=?=
Guest
Posts: n/a
 
      11th Sep 2007
Perfect! Thank You Very Much!

"JW" wrote:

> If x = y Then Selection.Rows(counter).EntireRow.Interior.ColorIndex =
> 6
>
> Lisab wrote:
> > PLEASE HELP!
> > I am trying to highlight every row in a worksheet that has a duplicate in
> > another worksheet in the same excel file.
> >
> > I have never programmed in excel but I am very skilled at VBA.
> >
> > I am using the following code. However, it errors out in the IF THEN
> > statement.
> >
> > ***Unable to set the Pattern property of the interior class**
> >
> > I also tried using the ColorIndex property and I get the same error-
> > Selection.Rows(counter).Interior.ColorIndex = vbYellow
> > ---------------
> > Sub Find_MatchesINZips()
> >
> > Dim compareRange As Variant
> > Dim x As Variant, y As Variant
> > Dim counter As Integer
> >
> > Set compareRange = Worksheets(2).Range("A2:A149")
> > counter = 1
> >
> > For Each x In Selection
> > For Each y In compareRange
> > If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow
> > Next y
> > counter = counter + 1
> > Next x
> >
> > End Sub

>
>

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      11th Sep 2007
Give this a whirl...

Sub Find_MatchesINZips()

Dim compareRange As Range
Dim x As Range, y As Range

Set compareRange = Worksheets(2).Range("A2:A149")

For Each x In Selection
For Each y In compareRange
If x.Value = y.Value Then y.EntireRow.Interior.ColorIndex = 6
Next y
Next x

End Sub
--
HTH...

Jim Thomlinson


"Lisab" wrote:

> PLEASE HELP!
> I am trying to highlight every row in a worksheet that has a duplicate in
> another worksheet in the same excel file.
>
> I have never programmed in excel but I am very skilled at VBA.
>
> I am using the following code. However, it errors out in the IF THEN
> statement.
>
> ***Unable to set the Pattern property of the interior class**
>
> I also tried using the ColorIndex property and I get the same error-
> Selection.Rows(counter).Interior.ColorIndex = vbYellow
> ---------------
> Sub Find_MatchesINZips()
>
> Dim compareRange As Variant
> Dim x As Variant, y As Variant
> Dim counter As Integer
>
> Set compareRange = Worksheets(2).Range("A2:A149")
> counter = 1
>
> For Each x In Selection
> For Each y In compareRange
> If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow
> Next y
> counter = counter + 1
> Next x
>
> End Sub

 
Reply With Quote
 
Bill Renaud
Guest
Posts: n/a
 
      11th Sep 2007
I added some variables to your code (it makes debugging much easier),
and used a Find method, which should run much faster than iterating
through each cell in the Compare range with a For loop. I also used the
Color property, instead of the ColorIndex property, which might not be
yellow, if the color pallette has been changed.

Sub FindMatchesInZips()
Dim rngCell As Range
Dim rngSelection As Range
Dim rngCompare As Range

Set rngSelection = Selection
Set rngCompare = Worksheets(2).Range("A2:A149")

For Each rngCell In rngSelection
If Not (rngCompare.Find(What:=rngCell.Value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows) Is Nothing) _
Then
rngCell.EntireRow.Interior.Color = vbYellow
End If
Next rngCell
End Sub
--
Regards,
Bill Renaud



 
Reply With Quote
 
=?Utf-8?B?TGlzYWI=?=
Guest
Posts: n/a
 
      11th Sep 2007
Thank You ALL, I love having more than one way of doing things.

What if I only wanted to highlight columns A: through I: and not the entire
row, what would be the syntax

"Bill Renaud" wrote:

> I added some variables to your code (it makes debugging much easier),
> and used a Find method, which should run much faster than iterating
> through each cell in the Compare range with a For loop. I also used the
> Color property, instead of the ColorIndex property, which might not be
> yellow, if the color pallette has been changed.
>
> Sub FindMatchesInZips()
> Dim rngCell As Range
> Dim rngSelection As Range
> Dim rngCompare As Range
>
> Set rngSelection = Selection
> Set rngCompare = Worksheets(2).Range("A2:A149")
>
> For Each rngCell In rngSelection
> If Not (rngCompare.Find(What:=rngCell.Value, _
> LookIn:=xlValues, _
> LookAt:=xlWhole, _
> SearchOrder:=xlByRows) Is Nothing) _
> Then
> rngCell.EntireRow.Interior.Color = vbYellow
> End If
> Next rngCell
> End Sub
> --
> Regards,
> Bill Renaud
>
>
>
>

 
Reply With Quote
 
=?Utf-8?B?TGlzYWI=?=
Guest
Posts: n/a
 
      11th Sep 2007
Can I add an AND to the code
I I take your code and modify it as follows, that highlight the rows in both
Sheet 1 and sheet 2?

Dim compareRange As Range
Dim x As Range, y As Range

Set compareRange = Worksheets(2).Range("A2:A149")

For Each x In Selection
For Each y In compareRange
If x.Value = y.Value Then y.EntireRow.Interior.ColorIndex = 6, _
AND x.entireRow.Interior.ColorIndex = 6
Next y
Next x


"Jim Thomlinson" wrote:

> Give this a whirl...
>
> Sub Find_MatchesINZips()
>
> Dim compareRange As Range
> Dim x As Range, y As Range
>
> Set compareRange = Worksheets(2).Range("A2:A149")
>
> For Each x In Selection
> For Each y In compareRange
> If x.Value = y.Value Then y.EntireRow.Interior.ColorIndex = 6
> Next y
> Next x
>
> End Sub
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Lisab" wrote:
>
> > PLEASE HELP!
> > I am trying to highlight every row in a worksheet that has a duplicate in
> > another worksheet in the same excel file.
> >
> > I have never programmed in excel but I am very skilled at VBA.
> >
> > I am using the following code. However, it errors out in the IF THEN
> > statement.
> >
> > ***Unable to set the Pattern property of the interior class**
> >
> > I also tried using the ColorIndex property and I get the same error-
> > Selection.Rows(counter).Interior.ColorIndex = vbYellow
> > ---------------
> > Sub Find_MatchesINZips()
> >
> > Dim compareRange As Variant
> > Dim x As Variant, y As Variant
> > Dim counter As Integer
> >
> > Set compareRange = Worksheets(2).Range("A2:A149")
> > counter = 1
> >
> > For Each x In Selection
> > For Each y In compareRange
> > If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow
> > Next y
> > counter = counter + 1
> > Next x
> >
> > End Sub

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      11th Sep 2007
And is a logical operator so that will not quite work out... try this...

Dim compareRange As Range
Dim x As Range, y As Range

Set compareRange = Worksheets(2).Range("A2:A149")

For Each x In Selection
For Each y In compareRange
If x.Value = y.Value Then
y.EntireRow.Interior.ColorIndex = 6
x.entireRow.Interior.ColorIndex = 6
End If
Next y
Next x

--
HTH...

Jim Thomlinson


"Lisab" wrote:

> Can I add an AND to the code
> I I take your code and modify it as follows, that highlight the rows in both
> Sheet 1 and sheet 2?
>
> Dim compareRange As Range
> Dim x As Range, y As Range
>
> Set compareRange = Worksheets(2).Range("A2:A149")
>
> For Each x In Selection
> For Each y In compareRange
> If x.Value = y.Value Then y.EntireRow.Interior.ColorIndex = 6, _
> AND x.entireRow.Interior.ColorIndex = 6
> Next y
> Next x
>
>
> "Jim Thomlinson" wrote:
>
> > Give this a whirl...
> >
> > Sub Find_MatchesINZips()
> >
> > Dim compareRange As Range
> > Dim x As Range, y As Range
> >
> > Set compareRange = Worksheets(2).Range("A2:A149")
> >
> > For Each x In Selection
> > For Each y In compareRange
> > If x.Value = y.Value Then y.EntireRow.Interior.ColorIndex = 6
> > Next y
> > Next x
> >
> > End Sub
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Lisab" wrote:
> >
> > > PLEASE HELP!
> > > I am trying to highlight every row in a worksheet that has a duplicate in
> > > another worksheet in the same excel file.
> > >
> > > I have never programmed in excel but I am very skilled at VBA.
> > >
> > > I am using the following code. However, it errors out in the IF THEN
> > > statement.
> > >
> > > ***Unable to set the Pattern property of the interior class**
> > >
> > > I also tried using the ColorIndex property and I get the same error-
> > > Selection.Rows(counter).Interior.ColorIndex = vbYellow
> > > ---------------
> > > Sub Find_MatchesINZips()
> > >
> > > Dim compareRange As Variant
> > > Dim x As Variant, y As Variant
> > > Dim counter As Integer
> > >
> > > Set compareRange = Worksheets(2).Range("A2:A149")
> > > counter = 1
> > >
> > > For Each x In Selection
> > > For Each y In compareRange
> > > If x = y Then Selection.Rows(counter).Interior.Pattern = vbYellow
> > > Next y
> > > counter = counter + 1
> > > Next x
> > >
> > > End Sub

 
Reply With Quote
 
Bill Renaud
Guest
Posts: n/a
 
      11th Sep 2007
More variables, more variables ... (sigh) (actually, you should love
variables like fruits & vegetables!!! Smile.)
I also used a more advanced technique to "locate" the actual Compare
range on the worksheet (which I named "ReferenceList" in my demo). This
is safer than using a fixed range (i.e.
"Worksheets(2).Range("A2:A149")"). What if the worksheet or the data
change name or size tomorrow? Also, I no longer assume that the
selection is in column $A. I generally use lots of ranges in my code,
because it is very difficult to write and debug otherwise.

Sub FindMatchesInZips()
Dim wsData As Worksheet
Dim rngCell As Range
Dim rngSelection As Range
Dim rngHighlight As Range

Dim wsCompare As Worksheet
Dim rngCompare As Range

Set rngSelection = Selection
Set wsData = rngSelection.Parent

Set wsCompare = Worksheets("ReferenceList")
With wsCompare.UsedRange
Set rngCompare = .Offset(1).Resize(.Rows.Count - 1)
End With

For Each rngCell In rngSelection
If Not (rngCompare.Find(What:=rngCell.Value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows) Is Nothing) _
Then
With rngCell
Set rngHighlight = wsData.Range("A" & .Row & ":I" & .Row)
rngHighlight.Interior.Color = vbYellow
End With
End If
Next rngCell
End Sub
--
Regards,
Bill Renaud


 
Reply With Quote
 
=?Utf-8?B?TGlzYWI=?=
Guest
Posts: n/a
 
      12th Sep 2007
Bill, thank you for all your help. I really appreciate it.

just as a side note- I tried using vbYellow but that is what caused my error
on my first couple of attempts. That is why I changed "vbYellow" to "6"

"Bill Renaud" wrote:

> More variables, more variables ... (sigh) (actually, you should love
> variables like fruits & vegetables!!! Smile.)
> I also used a more advanced technique to "locate" the actual Compare
> range on the worksheet (which I named "ReferenceList" in my demo). This
> is safer than using a fixed range (i.e.
> "Worksheets(2).Range("A2:A149")"). What if the worksheet or the data
> change name or size tomorrow? Also, I no longer assume that the
> selection is in column $A. I generally use lots of ranges in my code,
> because it is very difficult to write and debug otherwise.
>
> Sub FindMatchesInZips()
> Dim wsData As Worksheet
> Dim rngCell As Range
> Dim rngSelection As Range
> Dim rngHighlight As Range
>
> Dim wsCompare As Worksheet
> Dim rngCompare As Range
>
> Set rngSelection = Selection
> Set wsData = rngSelection.Parent
>
> Set wsCompare = Worksheets("ReferenceList")
> With wsCompare.UsedRange
> Set rngCompare = .Offset(1).Resize(.Rows.Count - 1)
> End With
>
> For Each rngCell In rngSelection
> If Not (rngCompare.Find(What:=rngCell.Value, _
> LookIn:=xlValues, _
> LookAt:=xlWhole, _
> SearchOrder:=xlByRows) Is Nothing) _
> Then
> With rngCell
> Set rngHighlight = wsData.Range("A" & .Row & ":I" & .Row)
> rngHighlight.Interior.Color = vbYellow
> End With
> End If
> Next rngCell
> End Sub
> --
> Regards,
> Bill Renaud
>
>
>

 
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 5 columns of text and highlight duplicates based on repeti BarryTheCamper Microsoft Excel Worksheet Functions 2 22nd Jan 2010 06:51 PM
Compare & highlight changes between two worksheets? Katie Microsoft Excel Worksheet Functions 2 7th Mar 2009 02:51 PM
Compare and highlight duplicates in two workbooks Dylan Microsoft Excel Programming 1 3rd Sep 2008 09:20 AM
compare two columns within a worksheet, then highlight duplicates =?Utf-8?B?YmV0aA==?= Microsoft Excel Worksheet Functions 1 20th Sep 2006 03:47 PM
Compare Two Worksheets and Highlight Changes TEAM Microsoft Excel Programming 3 18th May 2006 01:17 PM


Features
 

Advertising
 

Newsgroups
 


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