简体   繁体   中英

Conditional Formatting in Pivot Table in Excel based on text field

I would like to color code a pivot table based on a text field within the data.

This is a mock up, as I'm at a loss as to how this can be done. One of the tricky parts is that you can't actually display the text field that is the basis for the color.

Thank you, Neil

Contract Month PayStatus Payment
1 Jan-21 Complete 90
2 Jan-21 Complete 95
3 Jan-21 Complete 93
4 Jan-21 Complete 94
1 Feb-21 Pending 91
2 Feb-21 Complete 95
3 Feb-21 Complete 92
4 Feb-21 Complete 94
2 Mar-21 Complete 96
3 Mar-21 Pending 93
4 Mar-21 Pending 94
2 Apr-21 Complete 95
3 Apr-21 Pending 94

Mock-up Pivot Mock-up (color coded)

在此处输入图像描述

You still don't need to use VBA to get the results you're looking for, but there is a little work to do in order to set it up. The solution is to use Conditional Formatting in which the formula is VLOOKUP . To make it work, you need to modify your table slightly.

VLOOKUP works by finding a unique value in the first column of a table or range. The first column values in your table are not unique, nor are the values in the second column. But if you combine the first and second columns, you can create a unique value. A column must be added to the left of your data to create this unique value:

在此处输入图像描述

The formula in Column A combines the values in columns B and C:

=B2&"+"&TEXT(C2,"mmm")

(There's a reason to format the date as just the Month, more later)

What we're ultimately looking for in your formatting rules is when any value in the pivot table, for a given month and contract, matches either "Complete" or "Pending". Because we've set up the data with the helper column, we can now create a `VLOOKUP' that can work for us. So we need a formula that does this:

VLOOKUP( unique value based on month and contract , get the pay status ) = "Complete"

VLOOKUP( unique value based on month and contract , get the pay status ) = "Pending"

The formula in the VLOOKUP has to reference back to the source data (when it's inside the pivot table). In order to lookup that unique value, we have to create it by combining values from the pivot table. If the VLOOKUP formula was in Cell B6 in the pivot table:

在此处输入图像描述

Then the formula will be:

=VLOOKUP($A6&"+"&B$4,Sheet1!$A$1:$E$14,4,FALSE)="Complete"

Notice how the "lookup value" is not a single cell, but we're creating the same unique string/value in the helper column.

In your pivot table, click in the values area ("Sum of Payment") in Cell B6, then select Home -->Conditional Formatting-->Manage Rules. Next "Add New Rule" and then make sure your rule looks like this:

在此处输入图像描述

Create a second rule in the same manner and apply it to the pivot table.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM