Assuming the data is already sorted by Market column, as per your screenshot, in order to do the color formatting you could do something like:
Sub ColorMyTable()
Dim rng as range 'column range
Dim r as Long 'row iterator
Set rng = Activesheet.Range("E2",Range("E2").End(xlDown))
'## Assign some color to the last cell in this column
rng.Cells(1).Interior.ColorIndex = 39
For r = rng.Rows.Count to 1 Step - 1
With rng.Cells(r)
'Check to see if the cell is the same value as the one beneath it
If .Value = .Offset(1, 0).Value Then
'## Make them the same color
.Offset(1,0).Interior.ColorIndex = .Interior.ColorIndex
'
Else:
'## Code to apply a new semi-random color to the next row
' , when the values don't match.
.Offset(1, 0).Interior.ColorIndex = .Offset(1,0).Row Mod 55
End If
End With
Next
End Sub
This is the easiest, and also probably the ugliest, way I can think of to assign colors without any assignment in advance.
There is no check to see if the color has already been used, but I think this should come up with 55 different colors for you.
Unfortunately, unless you have some idea of what colors to use in advance, I can't think of a really easy way to assign colors. But here are a few more complicated methods you might try to adapt:
- You could for instance use a single color's long value, and then apply a particular
.TintAndShade
factor based on how many unique Asset's there are in the column. Like, this: How to assign gradient colors to an excel column? - On a similar note, you could assign a few colors as public variables, or use the built in constants from the color Theme (assuming Excel 2007+) and use a VBA function to rotate through these colors, also applying
.TintAndShade
, if necessary, just like the above, but with a bit more tweakability to use multiple colors. - You could use a random number generator to assign the color values, storing used colors in a collection, and checking for duplicate colors (and forcing a new color, if needed), etc.