Conditional format based on the first word

2019-09-21 15:52发布

问题:

Alba Botanica Hawaiian Hand + Body - 24 oz. - Replenishing Cocoa Butter
Alba Botanica Very Emollient Body Lotion - 32 oz. - Unscented Original
Aveeno Active Naturals Daily Moisturizer - 2 x 4 oz.
Aveeno Active Naturals Daily Moisturizing Lotion - 2 x 18 oz.
Aveeno Clear Complexion Favorites - 6 oz. Cleanser + 4 oz. Moisturizer
Cetaphil Moisturizing Lotion - 40 oz. - For All Skin Types
Clean & Clear Daily Skincare Essentials - 20 oz. - Cleanser + Moisturizer
Everyone Lotion For Every Body 3in1 - 32 oz. - Coconut + Lemon

This is my list of products, I'm trying to conditionally format so that each row that has a different first word has a different color. How do I do that?

回答1:

This is not perfect, but may be enough.

You can use the first x letters ascii code to create a numeric semi-unique ID, and then use it to color the cells. This could be done in 2 ways, but both start with making a new column (a "helper column") and paste the following formula at the first cell: CODE(LEFT(B1,1))+CODE(MID(B1,2,1))+CODE(MID(B1,3,1))+CODE(MID(B1,4,1))+CODE(MID(B1,5,1))+CODE(MID(B1,6,1))

This will convert the first 6 characters of each row (in column B) to ascii code, and sum them. If 6 characters are not enough you can use even more by adding +CODE(MID(B1,x,1)) where x is an increasing number.

After drugging this to all the column you'll get something like this:

  1. Using color range of conditional formatting:

This is the more simple and quick way, and may be even more suitable to your needs then the other one. Just use the range-color conditional formatting on the first column, and you will get a color based on the first word in every line:

  1. Using color index and VBA

The above solution does not color all the line, and if this is important to you, you can try adding another helper column with the formula:

=A1/MAX(A:A)*54

Where A is the first helper column. Then you can use this VBA code:

Sub format_text()
    Sheets(1).Select
    RowCount = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 3 To RowCount
        Cells(i - 2, 3).Interior.ColorIndex = Cells(i - 2, 2).Value + 2
        Next
    End
End Sub

to color column C (which is now the column with the text) with a color index between 3-56 of preset color in Excel (1 & 2 are black and white):

You may find that not all the colors are readable, but there is a limit to an effective color-coding (and it is about 7 colors), so this is an inherent problem of the question.

Tell me if it solved the problem ;)