PC Review


Reply
Thread Tools Rate Thread

Coloring duplicates based on values in a column.

 
 
=?Utf-8?B?Sm9zaA==?=
Guest
Posts: n/a
 
      2nd Jun 2007
Hello,
I have a spreadsheet with a macro I wrote in VBA to color duplicate
values. It works well but I have been trying to re-write the macro so that
the colors are alternating. Whereas it colors the first set of duplicates 1
color and then the second set of dupes a different color. Back and forth all
the way up the spreadsheet from the bottom of the UsedRange. Is there a way
to do this?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9zaA==?=
Guest
Posts: n/a
 
      2nd Jun 2007


"Josh" wrote:

> Hello,
> I have a spreadsheet with a macro I wrote in VBA to color duplicate
> values. It works well but I have been trying to re-write the macro so that
> the colors are alternating. Whereas it colors the first set of duplicates 1
> color and then the second set of dupes a different color. Back and forth all
> the way up the spreadsheet from the bottom of the UsedRange. Is there a way
> to do this?


I also forgot to put that I am coloring the EntireRow.

Thanks!
Josh
 
Reply With Quote
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      2nd Jun 2007
looks like you forgot to post the code you are using also. <g>

"Josh" wrote:

>
>
> "Josh" wrote:
>
> > Hello,
> > I have a spreadsheet with a macro I wrote in VBA to color duplicate
> > values. It works well but I have been trying to re-write the macro so that
> > the colors are alternating. Whereas it colors the first set of duplicates 1
> > color and then the second set of dupes a different color. Back and forth all
> > the way up the spreadsheet from the bottom of the UsedRange. Is there a way
> > to do this?

>
> I also forgot to put that I am coloring the EntireRow.
>
> Thanks!
> Josh

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      2nd Jun 2007
>> I have a spreadsheet with a macro I wrote in VBA to color duplicate
>> values. It works well but I have been trying to re-write the macro so
>> that
>> the colors are alternating. Whereas it colors the first set of duplicates
>> 1
>> color and then the second set of dupes a different color. Back and forth
>> all
>> the way up the spreadsheet from the bottom of the UsedRange. Is there a
>> way
>> to do this?

>
> I also forgot to put that I am coloring the EntireRow.


It sounds like you already have created the row coloring code, so I'll
assume that is in place. Below is a structure you can use to alternate
colors. Note that I have used a CommandButton Click event to house the code
structure, but you would place the code structure in whatever function or
procedure is appropriate for you macro.

Enum Colors
FirstColor = 15366924 ' RGB(012,123,234)
SecondColor = 818154 ' RGB(234,123,012)
End Enum

Private Sub CommandButton1_Click()
Static Color As Long
Const ColorSum As Long = Colors.FirstColor + Colors.SecondColor
If Color = 0 Then Color = Colors.FirstColor
Color = ColorSum - Color
'
' Your code that uses alternating colors goes here
'
End Sub

Here I have used to arbitrary colors to alternate between.... substitute
your own colors for the two I used (they only have to be set in the Enum).
You could, if you want, eliminate the Enum and place your color values
directly inside the procedure, but I think the code is more self-documenting
with the Enum. Anyway, each time the procedure containing this code
structure is executed, the static Color 'variable will contain the opposite
color from the one used the last time the procedure was executed.

Rick

 
Reply With Quote
 
=?Utf-8?B?Sm9zaA==?=
Guest
Posts: n/a
 
      2nd Jun 2007
Thanks Rick. Here is my code for coloring the dupes. I have just recently
been teaching myself VBA so I'm sure there would have been an easier way to
color the duplicates that is universal and not based on a specific column
within the code. Please take a look and see if there is something that can be
simplified and how your code would work within. Thanks.

Sub ColorAlternates()
Dim MyRange As String
Dim F As Boolean
Dim MyCount As Integer
Dim MyCount2 As Integer
Dim MyCount3 As Integer
MyCount = ActiveSheet.UsedRange.Rows.Count
MyCount2 = ActiveSheet.UsedRange.Rows.Count - 1
MyCount3 = ActiveSheet.UsedRange.Rows.Count
MsgBox (MyCount & " " & MyCount2)
Dim i As Integer
For i = MyCount To 3 Step -1
If Cells(MyCount, "B").Value = Cells(MyCount2, "B").Value Then
Cells(MyCount, "B").EntireRow.Interior.ColorIndex = 10
Cells(MyCount2, "B").EntireRow.Interior.ColorIndex = 10
End If
MyCount = MyCount - 1
MyCount2 = MyCount2 - 1
Next i
For i = MyCount3 To 3 Step -1
If Cells(MyCount3, "B").Value = Cells(MyCount3 - 1, "B").Value And
Cells(MyCount3, "B").EntireRow.Interior.ColorIndex = Cells(MyCount3 - 1,
"B").EntireRow.Interior.ColorIndex And F = True Then
Cells(MyCount, "B").EntireRow.Interior.ColorIndex = 15
Cells(MyCount, "B").EntireRow.Interior.ColorIndex = 15
F = False
GoTo EndLine
ElseIf Cells(MyCount3, "B").Value = Cells(MyCount3 - 1, "B").Value
And Cells(MyCount3, "B").EntireRow.Interior.ColorIndex = Cells(MyCount3 - 1,
"B").EntireRow.Interior.ColorIndex And F = False Then
Cells(MyCount3, "B").EntireRow.Interior.ColorIndex = 10
Cells(MyCount3, "B").EntireRow.Interior.ColorIndex = 10
F = True
End If
EndLine:
Next i
End Sub



"Rick Rothstein (MVP - VB)" wrote:

> >> I have a spreadsheet with a macro I wrote in VBA to color duplicate
> >> values. It works well but I have been trying to re-write the macro so
> >> that
> >> the colors are alternating. Whereas it colors the first set of duplicates
> >> 1
> >> color and then the second set of dupes a different color. Back and forth
> >> all
> >> the way up the spreadsheet from the bottom of the UsedRange. Is there a
> >> way
> >> to do this?

> >
> > I also forgot to put that I am coloring the EntireRow.

>
> It sounds like you already have created the row coloring code, so I'll
> assume that is in place. Below is a structure you can use to alternate
> colors. Note that I have used a CommandButton Click event to house the code
> structure, but you would place the code structure in whatever function or
> procedure is appropriate for you macro.
>
> Enum Colors
> FirstColor = 15366924 ' RGB(012,123,234)
> SecondColor = 818154 ' RGB(234,123,012)
> End Enum
>
> Private Sub CommandButton1_Click()
> Static Color As Long
> Const ColorSum As Long = Colors.FirstColor + Colors.SecondColor
> If Color = 0 Then Color = Colors.FirstColor
> Color = ColorSum - Color
> '
> ' Your code that uses alternating colors goes here
> '
> End Sub
>
> Here I have used to arbitrary colors to alternate between.... substitute
> your own colors for the two I used (they only have to be set in the Enum).
> You could, if you want, eliminate the Enum and place your color values
> directly inside the procedure, but I think the code is more self-documenting
> with the Enum. Anyway, each time the procedure containing this code
> structure is executed, the static Color 'variable will contain the opposite
> color from the one used the last time the procedure was executed.
>
> Rick
>
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      2nd Jun 2007
> Thanks Rick. Here is my code for coloring the dupes. I have just recently
> been teaching myself VBA so I'm sure there would have been an easier way
> to
> color the duplicates that is universal and not based on a specific column
> within the code. Please take a look and see if there is something that can
> be
> simplified and how your code would work within. Thanks.


Here is the concept behind my code (modified to account for how you are
selecting your colors and embedded directly into the subroutine) used to
alternately color rows 3 through 100 (your post said to start at 3; I chose
100 as a limit, but you can specify whatever row value you want in the For
loop's "To" value)...

Sub ColorAlternates()
Dim X As Long
Dim IndexValue As Long
' The sum of your two specified indexes - 10 and 15
Const ColorSum As Long = 25
' Specify the start color index
IndexValue = 10
For X = 3 To 100 ' Change 100 to maximum row value desired
Range("A" & X).EntireRow.Interior.ColorIndex = IndexValue
IndexValue = ColorSum - IndexValue
Next
End Sub


Rick

 
Reply With Quote
 
=?Utf-8?B?Sm9zaA==?=
Guest
Posts: n/a
 
      4th Jun 2007
Hi Rick,
Coloring alternate records isn't the problem...the problem is that I want to
color duplicate rows alternately. Let me know if this makes better sense,
thanks!

"Rick Rothstein (MVP - VB)" wrote:

> > Thanks Rick. Here is my code for coloring the dupes. I have just recently
> > been teaching myself VBA so I'm sure there would have been an easier way
> > to
> > color the duplicates that is universal and not based on a specific column
> > within the code. Please take a look and see if there is something that can
> > be
> > simplified and how your code would work within. Thanks.

>
> Here is the concept behind my code (modified to account for how you are
> selecting your colors and embedded directly into the subroutine) used to
> alternately color rows 3 through 100 (your post said to start at 3; I chose
> 100 as a limit, but you can specify whatever row value you want in the For
> loop's "To" value)...
>
> Sub ColorAlternates()
> Dim X As Long
> Dim IndexValue As Long
> ' The sum of your two specified indexes - 10 and 15
> Const ColorSum As Long = 25
> ' Specify the start color index
> IndexValue = 10
> For X = 3 To 100 ' Change 100 to maximum row value desired
> Range("A" & X).EntireRow.Interior.ColorIndex = IndexValue
> IndexValue = ColorSum - IndexValue
> Next
> End Sub
>
>
> Rick
>
>

 
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
Increment a row based on another column with duplicates possible Sabosis Microsoft Excel Programming 1 29th Mar 2011 06:05 AM
Filter duplicates based on criteria / column values phillr Microsoft Excel Misc 0 9th Apr 2010 09:13 PM
Remove Duplicates Based on more than one column =?Utf-8?B?TWljaGVsbGU=?= Microsoft Excel Programming 2 14th Jul 2007 01:45 AM
Conditional coloring of Excel cells, based on adjacent cell values? Greg Stuart Microsoft Excel Worksheet Functions 0 10th Mar 2006 10:14 PM
Calculating values to column D with formula based on values column A spolk Microsoft Excel Programming 1 30th Apr 2004 06:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:23 PM.