Automatically Sort Data in Alphabetical Order Using Formula
Automatically Sort Data in Alphabetical Order Using Formula
Excel built-in data sorting is amazing, but it isn’t dynamic. If you sort data and then add data to it, you INDEX- MATCH Combo
would need to sort it again.
Creating a Drop Down List
In this post, I will show you various ways to sort data in alphabetical order using formulas. This means
VBA Loops
you can add data, and it will automatically sort it for you.
In this example, all the data is in text format (no numbers, blanks or duplicates). To sort this, I will use a
helper column. In the column next to the data, use the following COUNTIF formula:
=COUNTIF($A$2:$A$9,"<="&A2)
This formula compares a text value with all the other text values and returns its relative rank. For
example, in cell B2, it returns 8, as there are 8 text values that are lower than or equal to the text ‘US’
(alphabetical order).
Now to sort the values, use the following combination of INDEX, MATCH and ROWS functions:
=INDEX($A$2:$A$9,MATCH(ROWS($B$2:B2),$B$2:$B$9,0))
This formula simply extracts the names in the alphabetical order. In the rst cell (C2), it looks for the
country name that has the lowest number (Australia has 1). In the second cell, it returns Canada (which
has the number 2) and so on..
Here is a formula that will do the same without the helper column.
=INDEX($A$2:$A$9,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9),0))
You have a mix of numbers and text (try putting 123 in one of the cells).
Now this one is a bit tricky. I will use 4 helper columns to show you how it works (and then give you a
huge formula that will do it without the helper columns). Suppose you have a data as shown below:
You can see there are duplicate values, blank and numbers. So I will use helper columns to address each
of these issues.
Helper Column 1
=COUNTIF($A$2:$A$9,"<="&A2)
Text and numbers are processed parallelly and this formula returns the same number for text and
number (for example 123 and India both get 1).
Helper Column 2
=--ISNUMBER(A2)
Helper Column 3
=--ISBLANK(A2)
Helper Column 4
=IF(ISNUMBER(A2),B2,IF(ISBLANK(A2),B2,B2+$C$10))+$D$10
The idea for this formula is to segregate blanks, numbers and text values.
If the cell is blank, it returns the value in cell B2 (which would always be 0) and adds the value in
cell D10. In a nutshell, it will return the total number of blank cells in the data
If the cell is a numerical value, it will return the comparative rank and add the total number of
blanks. For example, for 123 it returns 2 (1 is the rank of 123 in the data, and there is 1 blank cell)
If it is text, it returns the comparative rank and add the total number of numerical values and
blanks. For example, for India, it add the text’s comparative rank in text (which is 1) and adds the
number of blank cells and the number of numerical values.
Now we will use these helper columns to get the sorted list. Here is the formula:
=IFERROR(INDEX($A$2:$A$9,MATCH(SMALL($E$2:$E$9,ROWS($F$2:F2)+$D$10),$E$2:$E$9,
0)),"")
This method of sorting now becomes fool-proof. I have shown you the method for 8 items, but you can
extend it to as many items as you want.
If you can handle extreme formulas, here is an all-in-one formula that will sort data in alphabetical order
(without any helper column).
=IFERROR(INDEX($A$2:$A$9,MATCH(SMALL(NOT($A$2:$A$9="")*IF(ISNUMBER($A$2:$A$9)
,COUNTIF($A$2:$A$9,"<="&$A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9)+SUM(--
ISNUMBER($A$2:$A$9))),ROWS($A$2:A2)+SUM(--
ISBLANK($A$2:$A$9))),NOT($A$2:$A$9="")*IF(ISNUMBER($A$2:$A$9),COUNTIF($A$2:$A$9,"
<="&$A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9)+SUM(--
ISNUMBER($A$2:$A$9))),0)),"")
Enter this formula in a cell and drag it down to get the sorted list. Also, since this is an array formula, use
Control + Shift + Enter instead of Enter.
This formula has real-world utility. What do you think? I would love to learn from you. Leave your
footprints in the comments section!
Name
PedjaNBGD
If you want to extract unique values with ignoring blank cells use this formula:
{=IFERROR(INDEX($A$2:$A$9,MATCH(SMALL(IF(($A$2:$A$9″”)*
(MATCH($A$2:$A$9&””,$A$2:$A$9&””,0)=ROW($A$2:$A$9)-
ROW($A$2)+1),IF(ISNUMBER($A$2:$A$9),COUNTIF($A$2:$A$9,”<="&$A$2:$A$9),COUNTIF($
A$2:$A$9,"<="&$A$2:$A$9)+SUM(–
ISNUMBER($A$2:$A$9)))),ROWS($A$2:A2)),IF(($A$2:$A$9″”)*
(MATCH($A$2:$A$9&””,$A$2:$A$9&””,0)=ROW($A$2:$A$9)-
ROW($A$2)+1),IF(ISNUMBER($A$2:$A$9),COUNTIF($A$2:$A$9,”<="&$A$2:$A$9),COUNTIF($
A$2:$A$9,"<="&$A$2:$A$9)+SUM(–ISNUMBER($A$2:$A$9)))),0)),"")}
For reverse sorting data from ascending to descending replace SMALL( to LARGE(
Mike H
Jason
debashish ray
Jason Smith
Formula works great if the originating data list is not the result of formulas that may contain
blanks. I dont mean empty cells. I mean the result of a formula that returns a blank (“”). Even
the downloaded version puts the blanks on the top of the list (when changing the Column A to
show a few =””. Simply put – the formula doesnt work unless the originating data does not
contain “”. Shame too, it was a nice formula….until it didnt work. (If anyone figures this out, let
me know) (jason@simconconcrete.com)
Cardano
Method with helper columns also fails when I have duplicates… Will have to sort out a
solution…
Cardano
Solution was:
i) add a preliminary Helper 0 in column B, and shift rightward the rest of Helpers (ie.
Helper 1 in col.C, etc).
ii) in Helper 0, add the formula in cell B2
=COUNTIF($A$2:$A$9;”=”&A2) – COUNTIF($A$2:A2;”=”&A2)
… and extend to the end of the column range.
iii) adjust Helper 1 column with the formula in cell C2
=COUNTIF($A$2:$A$9;”<="&A2)-B2
Stephen
Ian Pritchett
Great work, thanks! Would have taken me a stupid amount of time to dope this out on my own
(and I LOVE helper columns)
Ian Pritchett
Glen of NYC
Above sort without helper column will fail with dupes. Use this following variation as the lazy
person solution to dupes. If column to be sorted has varying number of rows of data, this one
handles it…
=If(a2=””,””,INDEX($A$2:$A$9,IFERROR(MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$9,”<="&$
A$2:$A$9),0),MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9),-1))
Tuca
=IFERROR(INDEX($A$2:$A$9,MATCH(SMALL(NOT($A$2:$A$9=””)*IF(ISNUMBER($A$2:$A$9)
,COUNTIF($A$2:$A$9,”<="&$A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9)+SUM(–
ISNUMBER($A$2:$A$9))),ROWS($A$2:A2)+SUM(–
ISBLANK($A$2:$A$9))),NOT($A$2:$A$9="")*IF(ISNUMBER($A$2:$A$9),COUNTIF($A$2:$A$9,"
<="&$A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9)+SUM(–
ISNUMBER($A$2:$A$9))),0)),"")
Mike Raven
Wonderfully helpful — thank you. The way you’ve structured your explanation is excellent;
you’ve communicated the information with great clarity.
Dave
This only appears to work if the range is a text range rather than text returned from a formula.
If the range is cells with formulas it appears to sort the blank or zeros at the top rather than
the bottom. Is there a way to sort this?
Kenneth
I used your final formula (no helpers) but my data comes out at the bottom of my column and
not at top. HELP
Havana
Hi,
How would you work the above solution if there were names (first name and Surname of a
person)?
For example:
David Li
Matthew McLennan
Darren Findlay
Ben Maddahi
David Currie
None of the above methods seem to work (I am getting the rank formula value as 0)
scott
to handle duplicates, I just add the row number divided by a huge number to the rank. So,
something like =A4+row()/1000000000. This will make every rank unique.
Terence
I need to sort a names list alphabetically, primarily by the last name, together with the first
name and a student number. I obviously want the first name and student number to match up
to the last name after sorting. Is there are a way to set this up automatically so that as I paste
raw data into 3 columns – LAST NAME, FIRST NAME, STUDENT NUMBER, 3 formula columns
will fill-up automatically with the sorted data?
I have 100’s of students to sort this way each term and repeatedly using the “sort” function is
tedious.
Thank you
Me
Thank You!!
Share Reply to Me
Fred
How can I get the following formula to ignore cells which has a formula in, but appear blank?
=INDEX($A$2:$A$9,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$9,”<="&$A$2:$A$9),0))
Even better would be a formula that removes duplicates and sort the data alphabetically. This
source data is text only.
Paul N
Two things:
1 – The “One formula to sort if all” did not work for me as written. I had to make an edit:
“–ISBLANK” I had to change to “COUNTBLANK” and then it worked fine.
2 – Duplicates still appear in the result column. How can I have it remove duplicates in the
result column?
Thank you!
Elvis
Rob
Marium Ahmad
Hi Sumit. The forumala doesnt seem to work for me :/ When i click enter it become N/A. At
the bottom it says ciruclar reference can you please help?
Nikki
Hi i have the below formula, but it appear 0 in the cell, how can i make the cell in blank in
adjusting formula.
Thank you for your help.
=IF(ISERROR(INDEX(‘Oct-Dec2017’!$B$3:$V$350,SMALL(IF(‘Oct-
Dec2017’!$B$3:$B$392=MID(CELL(“filename”,X1),FIND(“]”,CELL(“filename”,X1))+1,256),ROW(
INDIRECT(“$1:$”&COUNTA(‘Oct-Dec2017′!$B$3:$B$392)))),ROW(1:1)),MATCH(C$3,’Oct-
Dec2017’!$B$3:$V$3,0))),””,INDEX(‘Oct-Dec2017’!$B$3:$V$392,SMALL(IF(‘Oct-
Dec2017’!$B$3:$B$392=MID(CELL(“filename”,X1),FIND(“]”,CELL(“filename”,X1))+1,256),ROW(
INDIRECT(“$1:$”&COUNTA(‘Oct-Dec2017′!$B$3:$B$392)))),ROW(1:1)),MATCH(C$3,’Oct-
Dec2017’!$B$3:$V$3,0)))
Cristian Mateo
Beautiful
Christopher Arabadjis
So helpful Sumit! This works great. I am using this to alphabetize a dynamic personnel list.
But when two people have the same last name, the first person shows up twice and the
second person is lost. Of course it takes another level of checking (first name will do). Any
advice?
Christopher Arabadjis
Eden Genet
trying to get a range of cells to change AUTOMATICALLY together by the last date and
arranging from oldest to newest date. (i.g. pallet total – row number – product date.)
I would think some sort of an array but any help would be most appreciated.
Martin
Well done, Sumit! Until I consulted your well-written article the only way I had thought of to
sort a table column without using the Sort command was to use a pivot table (which still
requires a refresh if the contents of the source column change). I had also tried using
conditional formatting to warn the user that the column was no longer sorted, but conditional
formatting rules break easily when you move the contents around. I assume your solution
works with Excel tables and will find out shortly. Also glad to see someone else uses the
=index(match(…)) combination, which I find more reliable than vlookup and lookup.
Martin
Actually, I needed to sort a two-column list, and borrowing your approach I couldn’t find a
way to do it without using a helper column. Have you figured out a way? Here’s an
example:
Letter Number
B3
A7
A5
B8
A4
B7
Sort the above list in order of Letter and Number so that it looks like this:
Letter Number
A4
A5
A7
B3
B7
B8
JR Ryan
Hi Sumit, Thank you for posting such a fantastic tutorial. I needed an array like this to sort
data from one source into another, and the “One Formula to Sort it All (without Helper
Columns)” solution was perfect once adapted.
One thing though, could you show me how to add a conditional filter to the match?
I have a sorting array (thank you ) in “Sheet 2” that works perfectly to grab unsorted data from
column “E” in the sheet “Level 4”, and then it sorts the data, drops any blanks and returns a
list sorted by “Last Name”. What I need to add to this array formula is the filtering of data
based on the “City” in column “G”.
=IFERROR(INDEX(‘Level 4’!$C$2:$G$110,MATCH(SMALL(NOT(‘Level
4’!$E$2:$E$110=””)*IF(ISNUMBER(‘Level 4’!$E$2:$E$110),COUNTIF(‘Level
4’!$E$2:$E$110,”<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level
4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),ROWS('Level
4'!$E$2:$E2)+SUM(–ISBLANK('Level 4'!$E$2:$E$110))),NOT('Level
4'!$E$2:$E$110="")*IF(ISNUMBER('Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"
<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level
4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),0),3),"")
This is what the unsorted data looks like on the "Level 4" sheet:
C | D | E | F | G
——————————————————————-
1| id number | First Name | Last Name | N/A | City
Where do I start?
JR Ryan
I’ve tried to carry on, but I am thoroughly stuck. I have changed the formula as follows,
and although it picks up the first match correctly, it doesn’t parse through the data to give
any other matches, just the same one over and over again.
=IFERROR(INDEX(‘Level 4’!$C$2:$G$110,MATCH(SMALL(–NOT(‘Level
4’!$G$2:$G$110=”Rome”)*NOT(‘Level 4’!$E$2:$E$110=””)*IF(ISNUMBER(‘Level
4’!$E$2:$E$110),COUNTIF(‘Level 4’!$E$2:$E$110,”<="&'Level
4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110)+SUM(–
ISNUMBER('Level 4'!$E$2:$E$110))),ROWS('Level 4'!$E$2:$G2)+SUM(–ISBLANK('Level
4'!$E$2:$E$110))),–NOT('Level 4'!$G$2:$G$110=Key!$G$6)*NOT('Level
4'!$E$2:$E$110="")*IF(ISNUMBER('Level 4'!$E$2:$E$110),COUNTIF('Level
4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level
4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),0),3),"")
JR Ryan
Sumit, please help. You wrote this formula. If anyone can solve this, it’s you!
Sumit Bansal
Sorry for a late response. Can you please share the example file (email at
sumitbansal@trumpexcel.com)
Abhishek Gupta
Sumit even i am facing same problem as prachi. can you share the sheet you have shared
with prachi. Prob by prachi was:
Hi..I am trying to use this formula but am facing a problem. My datasheet is different as i
have a list of names and the scores against it ( which obviously will have duplicates). I have
arranged that using helper 1 in a new column. If I use this formula, the same name gets
repeated for the same score. Please help with this problem. Am stuck at this since 4 days
now. Thanks in advance.
Please help
Sumit Bansal
Becky
Hi- first, you are brilliant- and this formula saved me! But, I have a question…I need the
associated data in the other columns to sort WITH my “Client Name” column which I want
auto-sorted in Alphabetic order so that I can add Clients without having to manually sort each
time. So, for example, I have “Client Originator” and “Client Owner” that is associated with
each client. When they auto sort, the Originator and Owner info needs to sort too. Help! Many
thx in advance.
Sumit Bansal
The better way would be to sort one column only, and based on the sorted data, use
VLOOKUP to fetch all the remaining records.
jeh
Hi Sumit, can you help me to have a formula for the automatic removal of the duplicate in one
colum, appreciate your help. thanks
jeh
Hi can someone help me, is there a formula for automatic remove duplicates
Sumit Bansal
HappyGuy
Trista Law
Hello,
What if I want to sort cells with on Column A which greater than zero on Column C?
Apple 1
Orange 1
Pear 0
Watermelon 2
Result:
Apple 1
Orange 1
Watermelon 2
Glen
This works great in the first 30-40 columns, but if I try to use it in column AW for example, it
doesn’t work. Any reason for that?
Niveditha Murugesan
Hey Sumith . Can u just help me in arranging from descending order? I kinda modified the
formula but dint work!
Arlen Lushi
Hi, great formula but I am having some difficulties. The blanks in my data set appear after the
last number in the data set therefore when I apply the formula the blanks appear at the top of
my sorted list. Is there a method to make these appear below the numbers in the sorted list?
Much appareciated!
Sumit Bansal
Hello Arlen, I tried replicating what you mentioned here and the blanks were at he
bottom. Have a look at image below
Arlen Lushi
Yes! that’s exactly what I need. Thanks … could you provide a solution please?
Sumit Bansal
Use the download file in the tutorial. You’ll get this in the second tab of the
workbook.
Russ
This formula is awesome! Can you share how you get the spaces to appear at the
bottom?
Sumit Bansal
James Cline
I am trying to sort my data that has been pasted into a spread sheet that will transpose to
another sheet and place the names in alphabetic order.
Sumit Bansal
Prachi Patel
Hi..I am trying to use this formula but am facing a problem. My datasheet is different as i
have a list of names and the scores against it ( which obviously will have duplicates). I have
arranged that using helper 1 in a new column. If I use this formula, the same name gets
repeated for the same score. Please help with this problem. Am stuck at this since 4 days
now. Thanks in advance.
Sumit Bansal
Prachi Patel
This is really awesome. Worked perfectly. Can you please explain what was the first
row about? Thanks a ton
Abhishek Gupta
SUMIT EVEN I AM FACING SAME PROBLEM LIKE PRACHI. CAN YOU SHARE THE
DATA
Ankur
Hey Sumit bhai you did a awesome job…. i have also made a formula …..
=IFERROR(INDEX($A$1:$A$15,MATCH(SMALL(IF(ISTEXT($A$1:$A$15),COUNTIF($A$1:$A$1
5,”<="&$A$1:$A$15)*15^2,IF(ISNUMBER($A$1:$A$15),COUNTIF($A$1:$A$15,"
<="&$A$1:$A$15))),ROW(1:1)),IF(ISTEXT($A$1:$A$15),COUNTIF($A$1:$A$15,"
<="&$A$1:$A$15)*15^2,IF(ISNUMBER($A$1:$A$15),COUNTIF($A$1:$A$15,"
<="&$A$1:$A$15))),0)),"")
NICE.
Sumit Bansal
Sukarno
Thanks so much
Sukarno
Thats a great tip indeed.. it really works for me, although I had to put lot of mind to
understand it.
Sumit Bansal
Raghu BK
Sumit Bansal
MF
Real world utility? I would guess it is good for feeding in a NamedRange for Data Validation,
so that you validating list always come in sorted order. What you say?
Share Reply to MF
Sumit Bansal
Thanks for commenting. Named range + Data validation would make an amazing use of
it. It can also be used to get the sorted data in another location/worksheet (in case of
employee names or any types of Ids). I got this query from a friend who wanted to sort id
names and numbers using a formula.
hubert
Sumit Bansal
Oana Ionita
Wow, thanks for this amaizing exemple!!! I have had really needed it.
Sumit Bansal
Thanks for commenting Oana.. This type of dynamic sorting helps save a lot of time. Glad
you find it useful
Pierre Sammut
Sumit Bansal
Dhiraj
Nice
Sumit Bansal
Rose
Sumit Bansal
Thanks for commenting Rose. I know the final formula is a bit too much to handle. But I
have tried making it robust to handle all situations (numbers, text, duplicates and blanks).
Ryan
Great tip! I don’t understand the big formula in the end, but it seems to do the trick! I am using
it going forward. Thanks for sharing
Sumit Bansal
Subscribe to comments
Privacy Policy | Sitemap