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

*Article updated on February 16, 2018*

**Table of Contents**

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

Filtering unique distinct text values and sort them based on sum of adjacent values can be done by creating a pivot table. But in this blog post I want to show how to do this using an Excel array formula.

**Array formula in D2:**

**How to create an array formula**

- Copy above array formula
- Select cell D2
- Click in formula bar
- Paste array formula
- Press and hold Ctrl + Shift
- Press Enter

**How to copy array formula**

- Select cell D2
- Copy cell D2 (Ctrl + )
- Select cell range D3:D7
- Paste (Ctrl + v)

**Formula in cell E2:**

### Download excel *.xlsx file

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

(Excel 97-2003 Workbook *.xls)

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

### Functions in this article:

**COUNTIF(**range,criteria**)**

Counts the number of cells within a range that meet the given condition

**IF(**logical_test;[value_if:true];[value_if_false]**)
**Checks whether a condition is met, and returns one value if TRUE, and another value if FALSE

**MATCH(**lookup_value;lookup_array; [match_type]

Returns the relative position of an item in an array that matches a specified value

**INDEX(**array,row_num,[column_num]**)**

Returns a value or reference of the cell at the intersection of a particular row and column, in a given range

**MAX(**number1,[number2],**)
**Returns the largest value in a set of values. Ignores logical values and text.

**ROW(**reference**)** returns the rownumber of a reference

**SUMIF(**range, criteria, sum_range**)**

Adds the cells specified by a given condition or criteria

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

### Leave a Reply

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

<code>your formula</code>

Remember to convert less than and larger than signs to html character entities before you post your comment.

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

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

VBA code

[/vb]

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

Upload picture to postimage.org

Add picture link to comment.

**Contact Oscar**

You can contact me through this webpage

[...] 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