Color Cell Based On Text Value

23

5

An Excel column contains a text value representing the category of that row.

Is there a way to format all cells having a distinct value a unique color without manually creating a conditional format for each value?

Example: If I had the categories bedroom, bedroom, bathroom, kitchen, living room, I would want all cells containing bedroom to be a particular color, bathroom a different color, etc.

Steven

Posted 2011-07-27T18:10:11.350

Reputation: 24 804

A lot of people here might also be interested in this related question: "How to change background color of cell based on other cell value by VBA": https://stackoverflow.com/questions/45955832/how-to-change-background-color-of-cell-based-on-other-cell-value-by-vba

– Ryan – 2019-11-21T19:46:11.103

If you dont want to do it manually, how do you want it decided what color things should be? – soandos – 2011-07-27T18:14:57.837

2I would like it automatic if possible, similar to how colors are chosen for different series in a chart. – Steven – 2011-07-27T18:27:26.513

Ah, so you want all cell with the same contents to be the same color, but dont care which color it is? – soandos – 2011-07-27T18:39:55.480

Would a one-time macro (to create the conditional formats once) be acceptable? It would need to be run exactly once per workbook and could be removed after that. – Tex Hex – 2011-07-27T19:58:40.560

soandos: Yes, TeX Hex: Sure! – Steven – 2011-07-27T20:14:40.683

Answers

6

  1. Copy the column you want to format to an empty worksheet.
  2. Select the column, and then choose "Remove Duplicates" from the "Data Tools" panel on the "Data" tab of the ribbon.
  3. To the right of your unique list of values or strings, make a unique list of numbers. For instance, if you have 6 categories to color, the second column could just be 1-6. This is your lookup table.
  4. In a new column, use VLOOKUP to map the text string to the new color.
  5. Apply conditional formatting based on the new numeric column.

Eric

Posted 2011-07-27T18:10:11.350

Reputation: 84

3Step 4 is a bit unclear to me, could you please elaborate? Thanks. – pixels – 2015-09-23T20:38:50.803

1Could you elaborate on 5? – zthomas.nc – 2017-03-26T17:37:54.103

1But surely this then means the formatting is on the cells containing the numeric value and NOT the text value – adolf garlic – 2018-02-15T10:39:11.883

11

The screenshots below are from Excel 2010, but should be the same for 2007.

Select the cell and go to Conditional Formatting | Highlight Cells Rules | Text that Contains

UPDATE: To apply the conditional formatting for the entire worksheet select all cells then apply the Conditional Formatting.

enter image description here
(Click image to enlarge)

Now Just select whatever formatting you want.

enter image description here

Nicu Zecheru

Posted 2011-07-27T18:10:11.350

Reputation: 5 234

So is it possible to have multiple rules for the 'text contains'? this is still pretty poor functionality from ms – adolf garlic – 2018-02-15T10:37:43.983

5Isn't this still going to require that the OP manually create a conditional format for each value? – Dave DuPlantis – 2011-07-27T20:12:29.223

1@Dave DuPlantis - NO. You can select ALL cells then use the conditional formatting. All cells that met the condition will be formatted accordingly. – Nicu Zecheru – 2011-07-28T11:06:27.210

7Each condition still has to be created manually, even though they only need to be created a single time for the entire workbook. He's looking for a solution that doesn't require him to specify the values. – Dave DuPlantis – 2011-07-28T11:24:18.170

2

From: http://www.mrexcel.com/forum/excel-questions/861678-highlighting-rows-random-colors-if-there-duplicates-one-column.html#post4185738

Sub ColourDuplicates()
Dim Rng As Range
Dim Cel As Range
Dim Cel2 As Range
Dim Colour As Long




Set Rng = Worksheets("Sheet1").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Rng.Interior.ColorIndex = xlNone
Colour = 6
For Each Cel In Rng


If WorksheetFunction.CountIf(Rng, Cel) > 1 And Cel.Interior.ColorIndex = xlNone Then
Set Cel2 = Rng.Find(Cel.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchDirection:=xlNext)
    If Not Cel2 Is Nothing Then
        Firstaddress = Cel2.Address
        Do
        Cel.Interior.ColorIndex = Colour
        Cel2.Interior.ColorIndex = Colour
            Set Cel2 = Rng.FindNext(Cel2)

        Loop While Firstaddress <> Cel2.Address
    End If




Colour = Colour + 1


End If
Next


End Sub

Karmo

Posted 2011-07-27T18:10:11.350

Reputation: 31

I see that I already upvoted this answer, but I can't find whatever code I ended up using. One day I'll eventually write some flexible code and share it here too. – Ryan – 2019-11-21T19:49:06.157

1

The automatic color choosing Conditional Formatting is not a feature of Microsoft Excel.

However, you can color an entire row based on the value of a category column individually.

  1. Create a New Formatting Rule in Conditional Formatting.
  2. Use a formula to determine which cells to format.
  3. Formula: =$B1="bedroom" (Assuming the category column is B)
  4. Set Format (using Fill color)
  5. Apply rule formatting to all cells

Steven

Posted 2011-07-27T18:10:11.350

Reputation: 24 804

2Fyi, Eric has posted a much more useful answer... yours instead looks like a rehash of the first answer you got. – Frank – 2015-07-31T14:53:19.563