View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
il_magpie il_magpie is offline
external usenet poster
 
Posts: 2
Default Coloring cells with same values using VBA?

Hi,

You could first pull a unique list of your Excel Column. Loop through
your unique list and compare each value in the excel column. If it
matches, color it, else skip. Here's some sample code: "Values" is the
range which has all the Excel column entries. "Extracts" is the range
to which unique values are extracted.

Sub Test()

Dim intUniqueCounter As Integer, intRowCounter As Integer,
intUniqueValue As Integer
Dim intColor As Integer

intColor = 5

'Filter unique list of values
Range("Values").AdvancedFilter Action:=xlFilterCopy,
CopyToRange:=Range("Extract" _
), Unique:=True

'Loop through each unique value
For intUniqueCounter = 1 To Range("Extract").CurrentRegion.Rows.Count
- 1

'Increment color with each unique value
intColor = intColor + 1
intUniqueValue =
Range("Extract").CurrentRegion.Cells(intUniqueCoun ter + 1, 1).Value

'Loop through each cell in column
For intRowCounter = 1 To Range("Values").CurrentRegion.Rows.Count
- 1

'Compare values
If Range("Values").Cells(intRowCounter + 1, 1).Value =
intUniqueValue Then

Range("Values").Cells(intRowCounter + 1, 1).Select
With Selection.Interior
.ColorIndex = intColor
.Pattern = xlSolid
End With
End If
Next
Next
End Sub


Hope this helps!

Varsha
Hi,

Say in an Excel column I have this:

B1 12
B2 12
B3 4
B4 7
B5 4
B6 12
B7 7

and want to use a VBA piece of code to give same color to those with
same value. How can I do so please?

Regards,
Mike