Thisisstackoverflow
Thisisstackoverflow

Reputation: 261

How to set the background of three cells based off of the value in one other cell, relatively?

I'm trying to make a formula that will color code three cells that are directly above/below each other. The color coding depends on the sum of three other cells that are directly above/below each other, and directly across from the cells to be colored.

This part is easy. The rule is:

Forumula: =SUM(H5:H7)>30

And it applies to:

$E$5:$E$7

However, the issue is that when I copy the cells that are being colored (E's) to three more cells below them for the next batch, the relative formula does not work as expected. It applies the formula on H6:H8 (+1) instead of H8:H10 (+3).

Is there a way to get the relativeformula to work as expected? I hope this was clear enough. I know it's hard to visual a spreadsheet, but ultimately I need the relative forumla to add +3 instead of +1 when it is copied.

Upvotes: 0

Views: 107

Answers (2)

TotsieMae
TotsieMae

Reputation: 835

A partial answer to your question has to do with your relative formula - =SUM(H5:H7) - under conditional formatting. Using this formula causes $E$5 to use =SUM(H5:H7) to determine its formatting, $E$6 to use =SUM(H6:H8) to determine its formatting, and so on. This ultimately leads to issues with the conditional formatting even before you begin copying the cells down.

EDIT: (piggy-backing off @Bharat Anand's answer)

@Bharat Anand's answer appears to be the best way to accomplish this question.

@Thisisstackoverflow requested clarification (under the accepted answer) and I'm hoping my screenshot below will help.

I set my worksheet up in what I believe is the exact same manner as @Bharat Anand, and it seems that the column letters may have been off. Setting mine up like this made it work correctly.

Excel visual of StackOverflow question of "How to set the background of three cells based off of the value in one other cell, relatively?"

EDIT: (clarification for future visitors)

I really liked @Bharat Anand's solution, so I wanted to explain how it works (using the images/formulas in the solution as reference):

The calculation in Column I is setup to return a number that will only appear three times, in succession, in Column I; in this way it kind of behaves like a unique identifier, allowing other formulas to reference these values for calculation purposes.

Three times is key for the original poster because they were looking for a range of three cells to be formatted based on a sum of three cells. For example, the formula in Column I can be modified to

=ROUNDDOWN((ROW()-b)/a,0)

where:

a is your desired number of cells to format/sum, and

b is equal to a - 1.


Using the formula in J5 as an example, this SUMIF formula uses I3:I7, which includes two (or b, as setup in my explanation above) rows above and two (b) rows below to decide if I5 equals any of the values found in I3:I7. It's important to include the two rows above and two rows below so that the range overlaps the same three cells to meet the requested requirements.

When I5 finds a match, the formula looks to Column H to sum those rows in H3:H7 where I3:I7 equals I5. In this example, J5, J6, and J7 will each equal 32.

Using the simple conditional formatting formula that was setup, we know that cells in Column E should be highlighted when the corresponding row in Column H is greater than 30. According to the original question, this should be done in ranges that are three (a) rows tall. We already handled for that, so all that was left was to make sure the conditional formatting spanned the data area in Column E.

I'm hoping I don't confuse and/offend anyone with this explanation, but it's how I understand it. I like the proposed solution and explaining it to myself again helps me solidify how it works.

Upvotes: 2

Bharat Anand
Bharat Anand

Reputation: 484

Problem:

If I correctly understand, what you are trying to achieve is to have the range E5:E7 formatted strictly based upon H5:H7, then the next three-cell range - E8:E10 - based upon H8:H10 and so on. You can do this by implementing the below solution:

Solution:

Step 1: Setup Columns as Below

Excel visual of StackOverflow question of "How to set the background of three cells based off of the value in one other cell, relatively?"


Step 2: Implement Conditional Formatting as Below

enter image description here


Explanation:

I have recreated your issue and I see your problem; it was not that obvious until I played around with changing values of different cells in Column H.

Apparently, this has to do with how Excel interprets your instructions (formula) supplied under conditional formatting. To understand this let's first focus on the behavior of cells E5, E6 and E7 as a function of values in Column H.

With the formula =SUM(H5:H7)>30 did you notice that:

  1. E5 format depends upon =SUM(H5:H7),
  2. E6 format depends upon =SUM(H6:H8), and
  3. E7 format depends upon =SUM(H7:H9) and

when you copy the range E5:E7 three cells down into E8:E10, the expected behavior is:

  1. E8 format depends upon =SUM(H8:H10),
  2. E9 format depends upon =SUM(H9:H11), and
  3. E10 format depends upon =SUM(H10:H12)

And this is exactly how the range E8:E10 will behave once the above solution is applied.

Upvotes: 2

Related Questions