Search code examples
excelexcel-formulahighlight

Excel - Highlight cell which is closer and smaller to a specific value


I would like to highlight a cell which is the closest the a specific value but still lower (below) or equal according to a formula.

For instance, if I have 14 in B4, I would like the cell 13 (or 14) highlighted but not 15 in the range.

The value I need to look for would be in B4 and the range that I must apply the highlight formula to would be L1:L371.

So far, the best result I was able to achieve was with (from what I was able to find using Google or Stackoverflow already existing content):

=SMALL($L1:$L317,COUNTIF($L1:$L371,"<="&$B$4))

Although, not only it selects the value I am looking for, it also selects all the values below.

May someone provide me some help so I can achieve the expected result please?

Thank you for your time and help, it is greatly appreciated.


Solution

  • Based on your description, it seems like you need to find the maximum value of a range (L1:L137) that is less than or equal to an input variable (B4) and highlight that value in the search range. Depending on what version of Excel you have, here's what you need to do:

    For Excel 2019 or Excel 365

    You can use the MAXIFS function. You would actually put that function in a conditional formatting rule, but more on that in just a second. As a test, put the following code in cell B5:

    =MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
    

    The first argument is your "MAX_RANGE", or the range that contains the values you want to find the max of. It is also our "CRITERIA_RANGE", the second argument. The third argument is the criteria itself, which is that the search range must be less than or equal to the value in cell B4. Essentially, we are going through the list and creating a subset of numbers that are less than or equal to our search value, and discarding the rest. I assume you are aware of relative vs. absolute referencing, since you used the "$" anchor in your referenced code. If not, here's a description of the difference.

    Now, the result of that formula should be the highest number in the list less than or equal to the search value, but that doesn't highlight it for us in our list. To do this we need conditional formatting. To do this, highlight your data range (L1:L137) and go to the conditional formatting drop down on the home tab of the menu ribbon. Choose "New Rule". On the dialog that pops up, choose, "Use a formula to determine which cells to format". In the formula input box, enter:

    =L1=MAXIFS($L$1:$L$137,$L$1:$L$137,"<="&$B$4)
    

    This will compare whether the value in the cell being evaluated (L1, for instance) is equal to the result of that formula we talked about above. Since L1 is only relatively referenced, this formula will work for every cell in the data range.

    Now, before you hit "OK" on the dialog, select the Format button. This will allow you to adjust your highlighting and formatting as you desire. Click "OK" on the formatting dialog, then "OK" on the Conditional Formatting Rule dialog. This should now highlight any data cell that is equal specifically to the result of our formula, and not everything that's less than our value.

    For Earlier Versions of Excel

    The concept is the same in earlier versions of Excel, but unfortunately, the MAXIFS function is not present in these versions. Instead, we must use an array formula. Array formulas are a whole other can of worms, but ExcelJet is an excellent resource. In fact, they talk about this very issue, here.

    Unfortunately, we can't put the array formula in the conditional formatting formula like we did above, so we'll need to put this formula on a cell in the worksheet, then the conditional formula should reference that new cell. So in cell B5, if you put:

    =MAX(IF(L1:L137=B4,L1:L137)
    

    And then, instead of pressing Enter, you must press Ctl + Shift + Enter

    This keyboard combination will tell Excel that you are trying to enter an array formula. If you don't press these keys, then the formula will error. Once you have entered the array formula, if you put your cursor in cell B5, you will see the formula bar at the top has added curly braces ({ , }) around the formula to look like

    {=MAX(IF(L1:L137=B4,L1:L137)}
    

    This leads to the same result as above, but is just achieved slightly differently. Now, following the same process described above for conditional formatting, you will simply set the formula to:

    =L1=$B$5
    

    And that should be it! Hope this helps!