Excel Tips

20
How to extract a unique distinct list from a column Overview Unique distinct values are all cell values but duplicate values are removed. Thanks to Eero, who contributed the original array formula! Example sheet - How to remove duplicate values Column A contains names, some cells have duplicate values. An array formula in column B extracts an unique distinct list from column A. Array formula in cell B2: =INDEX($A$2:$A$20, MATCH(0, COUNTIF($B$1:B1, $A$2:$A$20), 0)) Thanks, Eero! How to create an array formula 1. Copy above array formula. 2. Double click cell B2. 3. Paste (Ctrl + v).

description

useful for Excel users

Transcript of Excel Tips

Page 1: Excel Tips

How to extract a unique distinct list from a column

Overview

Unique distinct values are all cell values but duplicate values are removed.

Thanks to Eero, who contributed the original array formula!

Example sheet - How to remove duplicate values

Column A contains names, some cells have duplicate values.  An array formula in column B

extracts an unique distinct list from column A.

Array formula in cell B2:

=INDEX($A$2:$A$20, MATCH(0, COUNTIF($B$1:B1, $A$2:$A$20), 0))

Thanks, Eero!

How to create an array formula

1. Copy above array formula.

2. Double click cell B2.

3. Paste (Ctrl + v).

4. Press and hold Ctrl + Shift.

5. Press Enter.

Page 2: Excel Tips

6. Release all keys.

and copy cell B2 down as far as necessary.

Named ranges

In excel you can name a cell range, a constant or a formula. You can then use the named

range in a formula, making it easier for you to read and understand formulas.

Example

List : A2:A20

Tip! Use dynamic named ranges to automatically adjust cell ranges when new values are

added or removed.

How to create a named range

1. Select cell range A2:A20

2. Type List in name box

3. Press Enter

Array formula and named range in cell B2:

=INDEX(List,MATCH(0,COUNTIF($B$1:B1,List),0))

Excel 2007 users can remove errors using iferror() function:

Page 3: Excel Tips

=IFERROR(INDEX(List,MATCH(0,COUNTIF($B$1:B1,List),0)),"") + CTRL + SHIFT +

ENTER

and copy it down as far as necessary.

The formula is an array formula. To create an array formula you press Ctrl + Shift + Enter

after you have entered the formula.

Excel 2003 users can remove errors using isna() function:

=IF(ISNA(INDEX(List, MATCH(0, COUNTIF($B$1:B1, List), 0))), "", INDEX(List, MATCH(0,

COUNTIF($B$1:B1, List), 0))) + CTRL + SHIFT + ENTER

and copy it down as far as needed.

How to handle blank cells in a range

=INDEX(List,MATCH(0,IF(ISBLANK(List),"",COUNTIF($B$1:B1,List)),0)) + CTRL + SHIFT +

ENTER

and copy it down as far as needed.

Thanks Sean!

A somewhat shorter array formula:

=INDEX(List,MATCH(0,(List="")+COUNTIF($B$1:B1,List)),0)) + CTRL + SHIFT + ENTER

and copy it down as far as needed.

How the array formula in cell B2 works

Step 1 - Create an array with the same size as the list

=INDEX(List,MATCH(0,COUNTIF($B$1:B1,List),0))

COUNTIF(range,criteria)

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

COUNTIF($B$1:B1,List) returns an array containing either 1 or 0 based on if $B$1:B1 is

found somewhere in the array List .

COUNTIF($B$1:B1,List)

becomes

COUNTIF("Unique distinct list",{Federer,Roger; Djokovic,Novak; Murray,Andy;

Davydenko,Nikolay; Roddick,Andy; DelPotro,JuanMartin; Federer,Roger;

Davydenko,Nikolay; Verdasco,Fernando; Gonzalez,Fernando; Wawrinka,Stanislas;

Gonzalez,Fernando; Blake,James; Nalbandian,David; Robredo,Tommy;

Wawrinka,Stanislas; Cilic,Marin; Stepanek,Radek; Almagro,Nicolas} )

and returns:

{0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0}

Page 4: Excel Tips

This means the cell value in $B$1:B1 can´t be found in any of the cells in the named range

List. If it had been found, somewhere in the array the number 1 would exist.

Step 2 - Return the position  of an item that matches 0 (zero)

MATCH(lookup_value;lookup_array; [match_type] returns the relative position of an item in

an array that matches a specified value.

MATCH(0,COUNTIF($B$1:B1,List),0)

becomes

MATCH(0,{0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0},0)

and returns 1.

Step 3 - Return a cell 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.

=INDEX(List,1)

becomes

=INDEX({Federer,Roger; Djokovic,Novak; Murray,Andy; Davydenko,Nikolay; Roddick,Andy;

DelPotro,JuanMartin; Federer,Roger; Davydenko,Nikolay; Verdasco,Fernando;

Gonzalez,Fernando; Wawrinka,Stanislas; Gonzalez,Fernando; Blake,James;

Nalbandian,David; Robredo,Tommy; Wawrinka,Stanislas; Cilic,Marin; Stepanek,Radek;

Almagro,Nicolas}, 1)

and returns "Federer, Roger"

Relative and absolute cell references

When you copy the array formula down the countif formula range ($B$1:B1) expands. This is

created by using relative and absolute references.

The first cell, B2: COUNTIF($B$1:B1,List)

Second cell, B3: COUNTIF($B$1:B2,List)

and so on.

Create a dynamic ranged name

In this post I am going to explain the dynamic named range formula in Sam's comment. The

formula adds new rows and columns instantly to the named range. This makes the named

range dynamic meaning you don´t need to adjust cell references every time you add a new

row or column to the list. The formula takes care of only one list per sheet.

How to create a named range in excel 2007

1. Click "Formulas" tab on the ribbon.

Page 5: Excel Tips

2. Click "Name Manager".

3. Create a new named range and name it.

4. Type the formula below in "Refers to:" window:

5. Click close button.

Named range forumla:

=Sheet1!$A$1:INDEX(Sheet1!$1:$65535, COUNTA(Sheet1!$A:$A), COUNTA(Sheet1!

$1:$1))

Explaining formula

Step 1 - Count the number of cells in column A that are not empty

=Sheet1!$A$1:INDEX(Sheet1!$1:$65535, COUNTA(Sheet1!$A:$A), COUNTA(Sheet1!

$1:$1))

becomes

=Sheet1!$A$1:INDEX(Sheet1!$1:$65535, 4, COUNTA(Sheet1!$1:$1))

Step 2 - Count the number of cells in row 1 that are not empty

=Sheet1!$A$1:INDEX(Sheet1!$1:$65535, COUNTA(Sheet1!$A:$A), COUNTA(Sheet1!

$1:$1))

becomes

=Sheet1!$A$1:INDEX(Sheet1!$1:$65535, 4, 3)

Step 3 - Return a reference of the cell at the intersection of a particular row and

column

=Sheet1!$A$1:INDEX(Sheet1!$1:$65535, COUNTA(Sheet1!$A:$A), COUNTA(Sheet1!

$1:$1))

becomes

=Sheet1!$A$1:$C$4

This cell reference changes whenever new rows or columns are added or removed.

Page 6: Excel Tips

Possible scenarios when to use named ranges

Formulas, making them dynamic and easier to read and understand.

Charts (How to create a dynamic chart)

Pivot tables (Create a dynamic pivot table and refresh automatically in excel)

Functions in this article:

COUNTA(value1,[value2],)

Counts the number of cells in a range that are not empty

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

Creating a dependant drop down list containing unique distinct values

Here is a list of order numbers and products.

We are going to create two drop down lists.

The first drop down list contains unique distinct values from column A.

The second drop down list contains unique distinct values from column B, based on chosen

value in the first drop down list.

Create a dynamic named range

1. Click "Formulas" tab

2. Click "Name Manager"

3. Click "New..."

4. Type a name. I named it "order". (See attached file at the end of this post)

Page 7: Excel Tips

5. Type =OFFSET(Sheet1!$A$2,0,0,COUNTA(Sheet1!$A$2:$A$1000)) in  "Refers to:"

field.

6. Click "Close" button

Create a unique distinct list from column A

1. Select Sheet2

2. Select cell A2

3. Type "=INDEX(order,MATCH(0,COUNTIF($A$1:A1,order),0))" + CTRL + SHIFT +

ENTER

4. Copy cell A2 and paste it down as far as needed.

Create a dynamic named range to get unique distinct list

1. Select Sheet2

2. Click "Formulas" tab

3. Click "Name Manager"

4. Click "New..."

5. Type a name. I named it "uniqueorder". (See attached file at the end of this post)

6. Type =OFFSET(Sheet2!$A$2, 0, 0, COUNT(IF(Sheet2!$A$2:$A$1000="", "", 1)), 1)

in  "Refers to:" field.

7. Click "Close" button

Create drop down list

1. Select Sheet1

2. Select cell D2

3. Click Data tab

4. Click Data validation button

5. Click "Data validation..."

6. Select List in the "Allow:" window.

7. Type =uniqueorder in the "Source:" window

8. Click OK!

Here is a picture of what we have accomplished so far.

Page 8: Excel Tips

How to create a secondary unique list based on only one chosen cell value in

first drop down list

Create a dynamic named range

1. Click "Formulas" tab

2. Click "Name Manager"

3. Click "New..."

4. Type a name. I named it "product". (See attached file at the end of this post)

5. Type =OFFSET(Sheet1!$B$2,0,0,COUNTA(Sheet1!$B$2:$B$1000)) in  "Refers to:"

field.

6. Click "Close" button

Create a unique distinct list from column B

1. Select Sheet2

2. Select cell B2

3. Type "=INDEX(product, MATCH(0, COUNTIF($B$1:B1, product)+(order<>Sheet1!

$D$2), 0))" + CTRL + SHIFT + ENTER

4. Copy cell B2 and paste it down as far as needed.

Create a dynamic named range to get unique distinct list

1. Select Sheet2

2. Click "Formulas" tab

3. Click "Name Manager"

4. Click "New..."

5. Type a name. I named it "uniqueproduct". (See attached file at the end of this post)

Page 9: Excel Tips

6. Type =OFFSET(Sheet2!$B$2, 0, 0, COUNT(IF(Sheet2!$B$2:$B$1000="", "", 1)), 1)

in  "Refers to:" field.

7. Click "Close" button

Create drop down list

1. Select Sheet1

2. Select cell D5

3. Click Data tab

4. Click Data validation button

5. Click "Data validation..."

6. Select List in the "Allow:" window.

7. Type =uniqueproduct in the "Source:" window

8. Click OK!

Download example workbook

unique distinct dependent lists.xls

(Excel 97-2003 Workbook *.xls)

Download example workbook with a third column of data

unique-distinct-dependent-lists1 three columns.xls

(Excel 97-2003 Workbook *.xls)

 

Page 10: Excel Tips

Related article:

Create a drop down list containing only unique distinct alphabetically sorted text

values using excel array formula

Functions in this article:

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

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

COUNT(value1;[value2])

Counts the number of cells in a range that contain numbers

OFFSET(reference,rows,cols, [height],[width])

Returns a reference to a range that is a given number of rows and columns from a given

reference

MATCH(lookup_value;lookup_array; [match_type])

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

COUNTIF(range,criteria)

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

COUNTA(value1,[value2],)

Counts the number of cells in a range that are not empty

Dependent Drop Down Lists AddIn

Dependent drop down lists is an AddIn for Excel 2007/2010 that let´s you easily create drop

down lists (comboboxes, form controls) in Microsoft® Excel.

Page 11: Excel Tips

What are dependent drop down lists?

A dependent drop down list changes it´s values automatically depending on selected values

in previous drop down lists on the same row.

Example

The first drop down list in the picture above contains values from "Region" column. The

second drop down list contains values from "Country" column and the third from City column.

Now depending on the selected value in the first drop down list, the second and third drop

down lists change their values automatically.

In the picture above, the first drop down list has the value "Europe" selected, the second

"France and in the third you can choose between "Lyon" or "Paris". You can see how the

columns are related to each other if you examine the table above.

Features

Utilizes a pivot table to quickly filter and sort values for maximum speed

The drop down lists are populated using Visual Basic for Applications

No excel formulas

Easily copy values: Each drop down list is linked to the cell behind.

You can create as many drop down lists you want

You can send workbooks containing dependent drop down lists to  friends,

colleagues etc, as long as they can open macro-enabled workbooks.

For simplicity, your data set must be an excel table. That is easily created if you don´t know

how. A vba macro is required in your workbook. The AddIn shows you how in a few simple

steps.

Create a drop down list containing only unique distinct alphabetically sorted text values

Page 12: Excel Tips

Sort values using array formula

Array formula in cell B2

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

How to create an array formula

1. Select cell B2

2. Type above array formula

3. Press and hold Ctrl + Shift

4. Press Enter once

5. Release all keys

How to copy array formula

1. Select cell B2

2. Copy (Ctrl + c)

3. Select cell range B3:B6

4. Paste (Ctrl + v)

Explaining array formula in cell B2

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)

+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

Step 1 - Convert text to numbers

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)

+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

COUNTIF(range,criteria) counts the number of cells within a range that meet the given

condition

COUNTIF(List, ">"&List)+1

Page 13: Excel Tips

becomes

COUNTIF({"DD";"EE";"FF";"EE";"GG";"BB";"FF";"GG";"DD";"TT";"FF";"VV";"VV";"FF"},

">"&{"DD";"EE";"FF";"EE";"GG";"BB";"FF";"GG";"DD";"TT";"FF";"VV";"VV";"FF"})+1

becomes

{11;9;5;9;3;13;5;3;11;2;5;0;0;5}+1

becomes

{12;10;6;10;4;14;6;4;12;3;6;1;1;6}

Step 2 - Identify previous unique text values above current cell

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)

+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

COUNTIF(range,criteria) counts the number of cells within a range that meet the given

condition

NOT(COUNTIF($B$1:B1, List))

becomes

NOT(COUNTIF("Unique list sorted alpabetically",

{"DD";"EE";"FF";"EE";"GG";"BB";"FF";"GG";"DD";"TT";"FF";"VV";"VV";"FF"}))

becomes

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

becomes

{1;1;1;1;1;1;1;1;1;1;1;1;1;1}

Step 3 - Calculate maximum number in array

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List,

">"&List)+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)+1))

becomes

MAX({1;1;1;1;1;1;1;1;1;1;1;1;1;1}*({12;10;6;10;4;14;6;4;12;3;6;1;1;6})

and returns 14.

Step 4 - Convert maximum number into Boolean value

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List,

">"&List)+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)+1))=(COUNTIF(List,

">"&List)+1), 0, 1)

becomes

IF(14={12;10;6;10;4;14;6;4;12;3;6;1;1;6}, 0, 1)

and returns this array:

{1;1;1;1;1;0;1;1;1;1;1;1;1;1}

Step 4 - Return the relative position of an item in an array

Page 14: Excel Tips

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List,

">"&List)+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

MATCH(lookup_value;lookup_array; [match_type]) returns the relative position of an item in

an array that matches a specified value

MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)

+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0)

becomes

MATCH(0, {1;1;1;1;1;0;1;1;1;1;1;1;1;1}, 0)

and returns value 6.

Step 5 - Return a value of the cell at the intersection of a particular row and column

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

=INDEX(List, MATCH(0, IF(MAX(NOT(COUNTIF($B$1:B1, List))*(COUNTIF(List, ">"&List)

+1))=(COUNTIF(List, ">"&List)+1), 0, 1), 0))

becomes

=INDEX(List, 6)

becomes

=INDEX({"DD";"EE";"FF";"EE";"GG";"BB";"FF";"GG";"DD";"TT";"FF";"VV";"VV";"FF"}, 6)

and returns value BB.

Create a dynamic named range

1. Click "Formulas" tab

2. Click "Name Manager"

3. Click List

4. Type =OFFSET(Sheet1!$A$2, 0, 0, COUNT(IF(Sheet1!$A$2:$A$1000="", "", 1)), 1)

in  "Refers to:" field.

5. Click "Close" button

Page 15: Excel Tips

Named range

List (dynamic)

What is named ranges?

How to create a drop down list with values updated dynamically in excel 2007

1. Click Data tab

2. Click Data validation button

3. Click "Data validation..."

4. Select List in the "Allow:" window. See picture below.

5. Type =OFFSET($B$2, 0, 0, COUNT(IF($B$2:$B$1000="", "", 1)), 1) in the "Source:"

window

6. Click OK!

Page 16: Excel Tips

Download example workbook

Create-a-drop-down-list-containing-only-unique.xls

(Excel 97-2003 Workbook *.xls)

Functions in this article:

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

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

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

ROW(reference) returns the rownumber of a reference

MATCH(lookup_value;lookup_array; [match_type])

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

COUNTIF(range,criteria)

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

COUNT(value1;[value2])

Counts the number of cells in a range that contain numbers

OFFSET(reference,rows,cols, [height],[width])

Returns a reference to a range that is a given number of rows and columns from a given

reference

Page 17: Excel Tips

Sort values using vba

Array formula in cell B2:B8000:

=FilterUniqueSort($A$2:$A$8212)

How to create array formula

1. Select cell range B2:B8000

2. Type array formula above

3. Press and hold Ctrl + Shift

4. Press Enter once

5. Release all keys

VBA code

You can find the selectionsort function here: Using a Visual Basic Macro to Sort Arrays in

Microsoft Excel

Function FilterUniqueSort(rng As Range)

Dim ucoll As New Collection, Value As Variant, temp() As Variant

ReDim temp(0)

On Error Resume Next

For Each Value In rng

If Len(Value) > 0 Then ucoll.Add Value, CStr(Value)

Next Value

On Error GoTo 0

For Each Value In ucoll

temp(UBound(temp)) = Value

ReDim Preserve temp(UBound(temp) + 1)

Next Value

Page 18: Excel Tips

ReDim Preserve temp(UBound(temp) - 1)

SelectionSort temp

FilterUniqueSort = Application.Transpose(temp)

End Function

Function SelectionSort(TempArray As Variant)

Dim MaxVal As Variant

Dim MaxIndex As Integer

Dim i, j As Integer

' Step through the elements in the array starting with the

' last element in the array.

For i = UBound(TempArray) To 0 Step -1

' Set MaxVal to the element in the array and save the

' index of this element as MaxIndex.

MaxVal = TempArray(i)

MaxIndex = i

' Loop through the remaining elements to see if any is

' larger than MaxVal. If it is then set this element

' to be the new MaxVal.

For j = 0 To i

If TempArray(j) > MaxVal Then

MaxVal = TempArray(j)

MaxIndex = j

End If

Next j

' If the index of the largest element is not i, then

' exchange this element with element i.

If MaxIndex < i Then

TempArray(MaxIndex) = TempArray(i)

TempArray(i) = MaxVal

End If

Next i

End Function

Where to copy vba code?

1. Press Alt + F11

2. Insert a module into your workbook

3. Copy (Ctrl + c) above code into the code window

Download excel 97-2003 *,xls file