## Highlight closest number

This post demonstrates how to highlight records with the closest value to a criterion, you can also choose to highlight the second closest value (or more).

You can also extract the number using the formula described in this article: Find closest value, perhaps you are also interested in how to find the numbers in a total closest to a given sum.

### Conditional formatting formula

This formula won't work in Excel 2010 and later Excel versions, simply create a named range containing the formula above. Then use that name as the conditional formatting formula, get the file later in this article if you need to see exactly how I did it.

### How to apply conditional formatting formula

Make sure you adjust cell references to your excel sheet.

- Select cells B6:D10
- Press with left mouse button on "Home" tab
- Press with left mouse button on "Conditional Formatting" button
- Press with left mouse button on "New Rule.."
- Press with left mouse button on "Use a formula to determine which cells to format"
- Type
*=OR(ABS($C6-$C$2)=SMALL(ABS($C$6:$C$10-$C$2), ROW($A$1:INDEX($A:$A, $C$3))))*in "Format values where this formula is TRUE" window.

(*The formula displayed in the image above is not used in this article*) - Press with left mouse button on "Format.." button
- Press with left mouse button on "Fill" tab
- Select a color for highlighting cells.
- Press with left mouse button on "Ok"
- Press with left mouse button on "Ok"
- Press with left mouse button on "Ok"

### How the conditional formatting formula works in cell C6

=OR(ABS($C6-$C$2)=SMALL(ABS($C$6:$C$10-$C$2), ROW($A$1:INDEX($A:$A, $C$3))))

*Step 1 - Calculate age differences*

=OR(ABS($C6-$C$2)=SMALL(**ABS($C$6:$C$10-$C$2), **ROW($A$1:INDEX($A:$A, $C$3))))

ABS(number) returns the absolute value of a number, a number without its sign.

ABS($C$6:$C$10-$C$2)

becomes

ABS({25;46;33;26;47}-30)

becomes

ABS({-5;16;3;-4;17})

and returns {5;16;3;4;17}

**Step 2 - Create an array with the same size as chosen records to highlight in cell C3.**

=OR(ABS($C6-$C$2)=SMALL(ABS($C$6:$C$10-$C$2),**ROW($A$1:INDEX($A:$A,$C$3)))**)

ROW($A$1:INDEX($A:$A,$C$3)))

becomes

ROW($A$1:INDEX($A:$A,2)))

becomes

ROW($A$1:$A$2)

and returns {1;2}

*Step 3 - Return the two smallest numbers in array*

=OR(ABS($C6-$C$2)=**SMALL(ABS($C$6:$C$10-$C$2),ROW($A$1:INDEX($A:$A,$C$3)))**)

SMALL(array,k) returns the k-th smallest number in this data set.

SMALL(ABS($C$6:$C$10-$C$2),ROW($A$1:INDEX($A:$A,$C$3)))

becomes

SMALL({5;16;3;4;17};{1;2})

and returns {3;4}

*Step 4 - Is cell value in C6 equal to any number in array?*

=OR(ABS($C6-$C$2)=SMALL(ABS($C$6:$C$10-$C$2),ROW($A$1:INDEX($A:$A,$C$3))))

becomes

OR(logical1, logical2, ...) checks whether any of the arguments are TRUE, and returns TRUE or FALSE. Returns FALSE if only all arguments are FALSE.

$C6 is a relative AND absolute cell reference.

=OR(ABS($C6-$C$2)={3;4})

becomes

=OR(ABS(25-30)={3;4})

becomes

=OR(ABS(-5)={3;4})

becomes

=OR(5={3;4})

becomes

=OR({5=3,5=4})

becomes

=OR({FALSE,FALSE})

and returns FALSE. Row 6 is not highlighted.

### Cf min max category

The image above shows rows highlighted based on value in column C being the largest or smallest in that particular […]

Question: How do I highlight the smallest duplicate value in a column using conditional formatting? Answer: Conditional formatting formula in […]

### Conditional formatting category

The image above demonstrates a conditional formatting formula that colors a record if there is at least one record that […]

This article explains how to count cells highlighted with Conditional Formatting (CF). The image above shows data in cell range […]

adam asks: Hi, I have a situation where I want to count if this value is duplicate and if it […]

Question: How do I highlight dates that meet criteria using conditional formatting? Table of contents Highlight values in a column […]

The image above shows rows highlighted based on value in column C being the largest or smallest in that particular […]

This article shows you how to easily identify duplicate rows or records in a list. What's on this webpage Conditional […]

The image above demonstrates a conditional formatting formula applied to an Excel Table containing random data. The Excel Table has […]

Here is how to highlight every other row using conditional formatting. Conditional formatting formula: =ISEVEN(ROW())*OR($B3:$D3<>"") Alternative CF formula: =EVEN(ROW())=ROW() This […]

Question: I have a list that I keep adding rows to. How do i create a border that expands as […]

The image above shows you how to highlight rows with multiple criteria using OR logic. The criteria are found in […]

In this article, I will demonstrate how to search a table using conditional formatting. The criteria highlight matching column and […]

Today I am going to show you how to quickly compare two tables using Conditional Formatting (CF). I am going […]

The image above demonstrates a conditional formatting formula that highlights duplicate items based on date. The first instance is not highlighted, […]

A conditional formatting formula highlights values in column B that also exist in column D. =COUNTIF($D$3:$D$7,B3) The same thing happens […]

In this post I am going to try to explain formula basics in conditional formatting. It is really good if […]

The image above shows conditional formatting highlighting unique distinct values, duplicates are not highlighted. Conditional Formatting Formula: =COUNTIF($B$3:B3, B3)=1 The […]

This article demonstrates how to apply different cell formatting to a cell range based on a Drop Down list, column […]

In this blog post I will demonstrate a conditional formatting formula that will highlight common records in two lists. The […]

The image above demonstrates a conditional formatting formula that highlights records that only exist in one table. There are two […]

## Functions in this article

More than 1300 Excel formulas

## Conditional Formatting categories

## Excel categories

### 12 Responses to “Highlight closest number”

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

I duplicated the spreadsheet but the formula did not work for me on Excel 2007. It says there's an error. Could you check and explain? Thx.

Lyon,

You probably have to adjust cell references in conditional formatting formula to your spreadsheet.

As I mentioned, I recreate the sample spreadsheet and copy paste the formula. But Excel 2007 says there's an error in the formula. Something is not right.

Lyon,

You are right, I found errors. I have corrected them.

Thanks for pointing them out.

It's now working! Great! Thanks a lot for your quick response.

Absoultely great

only small problem

when I enter value of 40 in search value in example given

it highlights 3 values (wendy also) I accepted it to highlight

only richard & sarah

I used this formula, however, I received errors. I see this post is quite old. is there an updated formula for excel 2010?

kris

You are right, it is not working for excel 2010.

I created a named range with the same formula and used that named range in a CF formula.

Please can you elaborate as to how to get this done in excel 2013.

Ranjan

See my answer to kris above, it should work in excel 2013 also?

How can I copy this conditional formatting formula to other cells WITHOUT having to adapt the $cells?

Is there any option to use the highlighting formular with a relative cell reference? This would make it much easier to copy the format...

Best regards,

Franz

Franz,

To change the range of cells that the conditional formatting rules applies to: https://spreadsheeto.com/conditional-formatting/#editingrule