## Extract unique distinct values sorted based on sum of adjacent values

Filtering unique distinct text values and sort them based on the sum of adjacent values can easily be done by creating a pivot table, and I highly recommend it.

But in this blog post I want to show how to do this using an Excel array formula.

Array formula in E2:

**How to create an array formula**

- Copy above array formula
- Double click cell E3
- Paste array formula
- Press and hold Ctrl + Shift simultaneouslöy
- Press Enter

Formula in cell F3:

### Explaining formula in cell E2

#### Step 1 - Count prior values above current cell

The COUNTIF function counts values based on a condition or criteria, if number is 0 (zero) then the corresponding value has not yet been displayed.

NOT(COUNTIF($D$1:D1, $A$2:$A$8))

becomes

NOT(COUNTIF("Unique distinct", {"AA";"BB";"CC";"BB";"CC";"AA";"DD"}))

becomes

NOT({0;0;0;0;0;0;0})

The NOT function returns the boolean opposite to the given argument. The array contains no boolean values, however it does contain their numerical equivavents. TRUE = 1 and FALSE = 0 (zero).

NOT({0;0;0;0;0;0;0})

and returns

{TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE}.

#### Step 2 - IF TRUE then return the corresponding sum

The IF function returns the total if boolean value is TRUE. FALSE returns "" (nothing).

IF(NOT(COUNTIF($E$2:E2, $B$3:$B$9)),SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),"")

becomes

IF({TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE}, SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),"")

The SUMIF function adds numbers and returns a total based on a condition or criteria.

IF({TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE}, SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),"")

becomes

IF({TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE}, {80;100;110;100;110;80;100},"")

and returns

{80;100;110;100;110;80;100}.

#### Step 3 - Get the largest number in array

The MAX function returns the largest number in array ignoring blanks and text values.

MAX(IF(NOT(COUNTIF($E$2:E2, $B$3:$B$9)),SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),""))

becomes

MAX({80;100;110;100;110;80;100})

and returns 110.

#### Step 4 - Match number

The MATCH function returns the relative position of a value in a cell range or array.

MATCH(MAX(IF(NOT(COUNTIF($E$2:E2, $B$3:$B$9)),SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),"")), IF(NOT(COUNTIF($E$2:E2, $B$3:$B$9)),SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),""), 0)

becomes

MATCH(110, IF(NOT(COUNTIF($E$2:E2, $B$3:$B$9)),SUMIF($B$3:$B$9, "="&$B$3:$B$9, $C$3:$C$9),""), 0)

becomes

MATCH(110, {80;100;110;100;110;80;100}, 0)

and returns 3.

#### Step 5 - Get value

The INDEX function returns a value based on row number (and column number if needed)

INDEX($A$2:$A$8, MATCH(MAX(IF(NOT(COUNTIF($D$1:D1, $A$2:$A$8)),SUMIF($A$2:$A$8, "="&$A$2:$A$8, $B$2:$B$8),"")), IF(NOT(COUNTIF($D$1:D1, $A$2:$A$8)),SUMIF($A$2:$A$8, "="&$A$2:$A$8, $B$2:$B$8),""), 0))

becomes

INDEX($A$2:$A$8, 3)

and returns "CC" in cell E2.

### Download Excel *.xlsx file

Filter unique distinct list sorted based on sum of adjacent values.xlsx

### Filtering unique distinct text values and sort them based on sum of adjacent values using a criteria list

**Array formula in cell E7:**

**Formula in cell E7:**

### Download excel *.xlsx file

filter-unique-distinct-list-sorted-based-on-sum-of-adjacent-values-using-array-formula2.xlsx

5 easy ways to extract Unique Distinct Values

First, let me explain the difference between unique values and unique distinct values, it is important you know the difference […]

Extract a unique distinct list from two columns

Question: I have two ranges or lists (List1 and List2) from where I would like to extract a unique distinct […]

Vlookup – Return multiple unique distinct values

Ahmed Ali asks: How to return multiple values using vlookup in excel and removing duplicates? I have tried the formula […]

Create a unique distinct alphabetically sorted list

The array formula in cell D3 extracts unique distinct values sorted A to Z, from column B to column D. […]

Extract a unique distinct list and sum amounts based on a condition

Anura asks: Is it possible to extend this by matching items that meet a criteria? I have a list of […]

Extract a unique distinct list from three columns

Question: How do I extract a unique distinct list from three ranges or lists? The ranges are not necessarily adjacent […]

Extract a unique distinct list and remove blanks

Question: How do I create a unique distinct list from a list containing several blanks? Answer: Cell range B3:B12 contains several […]

Extract unique distinct values from a multi-column cell range

Question: I have cell values spanning over several columns and I want to create a unique list from that range. […]

Extract unique distinct values A to Z from a range and ignore blanks

This is an answer to a question in this blog post: Extract a unique distinct list sorted from A-Z from […]

How to create a unique distinct list based on two conditions

Question: How do I create a unique distinct list where other columns meet two criteria using excel array formula? Answer: […]

### 11 Responses to “Extract unique distinct values sorted based on sum of adjacent values”

### 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

[...] Filter unique distinct list sorted based on sum of adjacent values … [...]

How might I go about doing this if the values range from positive to negative. ie.

Text Number

AA 60

BB 90

CC 80

EE -100

CC 30

FF -50

DD 100

Eric,

Array formula in A11:

=INDEX(List_text, MATCH(LARGE(IF(COUNTIF($A$10:A10, List_text)=0, SUMIF(List_text, "="&List_text, List_number), ""), 1), SUMIF(List_text, "="&List_text, List_number)*NOT(COUNTIF($A$10:A10, List_text)), 0)) + CTRL + SHIFT + ENTER. Copy cell A11 and paste it down as far as necessary.

Hi Oscar,

Thanks for the formula above, it fixes the case where there are negative values. But, I'm seeing one error - whenever the sum of numbers for some entry is Zero, then the formula fails. In Eric's example set above, if number corresponding to DD was 0, then your formula stops to work. Can you pls suggest something to fix this? Many thanks in adv, appreciate if you could respond soon. Cheers.

William,

You are right! I have changed this post. I hope it works better now.

Thanks for commenting!

What if i wanted sum value to be sorted in descending order.

Oscar, one more question.

How to modify this formula, to use other unique criteria list array being on another worksheet? For example: unique values match only are - AA and BB from criteria list and A1:A12 range from your example.

Can you help me?

Bill,

read this:

Filtering unique distinct text values and sort them based on sum of adjacent values using a criteria list

Thank you, Oscar!

Hi, Oscar,

So what if to do this single array formula? Is it real? Without prepared criteria list. The criteria list mixed with the others

values. How to extract the same values from other range contained in two ranges and sorted by SUM using single formula?

Please help me, I need to assist someone in the processing of their petty cash so i require the following:

I have certain accounts that represent only an income or expense so what i want to know is if i have columns that Reference; Date; Account; Invoice#; Description; PAID; Cash+- and Balance

Based on the Account number that is used (numeric) I want it to return the value that is in PAID column as ZARfigure into Cash+- as positive or negative based on the account code used: EXAMPLE:

Referance P-003414

Date 18.10.2017

Account 2200 (only expenses run through this account all amounts need to be negative

Invoice # 00001

Description Supplier Payment

Paid 1200

Cash In/out -1200

Balance -1200 - This is a running total

There are 18 accounts that i use in total for this purpose and 14 are Negative and 4 are positive

Thank you

Tamara