Can you help me sort my data correctly?

JRRyan

Board Regular
Joined
Jul 12, 2010
Messages
55
Hi all,

so, I've created a set of arrays (sheet 2) to filter, sort, and remove blanks from across two columns (first name, last name) of a larger data set (level 4) and I'm outputting the results into a single column sorted alphabetically by last name.

I have used a set of four helper columns in sheet 2 to achieve this, but at the point where I sort the filtered data from columns B and C into column D I find that if there are two unique records with the same last names, it will enter the first instance twice instead of once each for both.

I have made a sample spreadsheet here's a download link:

http://aubilltech.weebly.com/uploads/4/1/1/8/4118723/filter_and_sort_test.xlsx

Also, I have a unique record ID column that could come in handy in column A, but I'd prefer to avoid using even more helper columns.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Wow, your D2 formula is . . . incredible? I'm not even going to try to figure that out. Let's just jump to what you want as a final output. Your A/B/C columns simply extract the data from the 'Level 4' sheet that match the city and have a Y in column A. Then you have your D and E formulas, just designed to create a sorted list from A:C?

So do you want columns A:D, or are you happy just with the final list? Are all the IDs 7 digits? Would you be happy with different helper columns? I believe this can be done with formulas, but what are your feelings toward VBA?
 
Last edited:
Upvote 0
Wow, your D2 formula is . . . incredible?


Thanks...? haha:D

You've described it pretty accurately. The formula in D2:DX is unwieldy, partly because of the error checking.

I would love to do this all in one column, but I found that it would break the sort order if the original data set was changed or refiltered in any way (which it is, frequently). Unfortunately, I can't use VBA as the network restricts macro-enabled spreadsheets. So, if you could do all that in a single formula, I would be very impressed!

In the meantime, just getting the alphabetical sort to work properly is my main priority. The more I think about it, the more I believe that the ID will have to be used somehow.
 
Upvote 0
Consider this:

ABCD
1ILL4_MadridSortedFirst/Last
229Mumduya,WilmaAnnatoo,JuanJuan Annatoo
330Hyman,BusterAnthropist ,PhilPhil Anthropist
431Cherry,PoppaAppeno ,HalHal Appeno
532Lee,ZackBalls,SaulSaul Balls
633Stairs,DonCade,BarryBarry Cade
734Balls,SaulCherry,PoppaPoppa Cherry
835Pants,Peter*******,GrahamGraham *******
936Appeno ,HalDente,AlAl Dente
1037Matic,OttoFicial,ArtyArty Ficial
1138Fugga,MoeFoolery,TomTom Foolery
1239*******,GrahamFugga,MoeMoe Fugga
1340Foolery,TomGardens,MarvinMarvin Gardens
1441Dente,AlGraham,HollyHolly Graham
1542Wiser,BudHarmonic ,PhilPhil Harmonic
1643Graham,HollyHyman,BusterBuster Hyman
1744Stein,FrankLee,ZackZack Lee
1845Toe,CamMatic,OttoOtto Matic
1946Zona,TaraMumduya,WilmaWilma Mumduya
2047Cade,BarryNobatti,DonatellaDonatella Nobatti
2148Anthropist ,PhilPants,PeterPeter Pants
2249Gardens,MarvinStairs,DonDon Stairs
2350Harmonic ,PhilStein,FrankFrank Stein
2451Ficial,ArtyToe,CamCam Toe
2553Nobatti,DonatellaWiser,BudBud Wiser
2654Annatoo,JuanZona,TaraTara Zona
27

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
B2=IF(A2="","",INDEX('Level 4'!$E$2:$E$126,A2)&","&INDEX('Level 4'!$D$2:$D$126,A2))
D2=IFERROR(MID(C2,FIND(",",C2)+1,99)&" "&LEFT(C2,FIND(",",C2)-1),"")

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Array Formulas
CellFormula
A2{=IFERROR(SMALL(IF('Level 4'!$G$2:$G$126=$B$1,IF('Level 4'!$A$2:$A$126="Y",ROW('Level 4'!$C$2:$C$126)-ROW('Level 4'!C$2)+1)),ROWS($A$2:$A2)),"")}
C2{=IFERROR(INDEX($B$2:$B$126,MATCH(SMALL(COUNTIF($B$2:$B$126,"<"&$B$2:$B$126),ROWS($C$2:$C2)+COUNTBLANK($B$2:$B$126)),COUNTIF($B$2:$B$126,"<"&$B$2:$B$126),0)),"")}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Column A is just the row number of matches from the 'Level 4' sheet. Column B just uses that index to create a last name/first name list. Column C sorts column B. And finally, column D just rearranges the last/first names.

As far as doing it in a single column? Well, it would be really long and horribly inefficient, but I think it would be possible. But see how this works first.
 
Upvote 0
That's a really interesting way of looking at it! Very elegant solution. I don't use character/cell instructions much. It never occurred to me to sort by last name first and split the order mid cell. Nice way of avoiding the blanks as well. Thank you!
 
Upvote 0
If I'm understanding, the requirement is for a sorted list of unique [Distinct] names for a particular City. A query can do this without formulas, without VBA.

A parameterised query can automatically update each time the cell containing the city value changes. Here is a good example, http://dailydoseofexcel.com/archives/2004/12/13/parameters-in-excel-external-data-queries/

For the specific data, the SQL could be,
Code:
SELECT DISTINCT [First Name] & ' ' & [Last Name]
FROM [Level 4$]
WHERE City = [?]
 
Upvote 0
I belatedly noticed that the sort order wanted was by last name & then first name. The solution I offered earlier did not address that.

Simple tweak but returning "last name, first name" would be
Rich (BB code):
SELECT DISTINCT [Last Name] & ', ' & [First Name] AS [Full Name]
FROM [Level 4$]
WHERE City = [?]]


Or, untested (I'm assuming the parameterised value is OK down one level)), to sort as was asked (by last name & then first name with names returned 'first name last name')
Rich (BB code):
SELECT [First Name] & ' ' & [Last Name] AS [Full Name]
FROM (
SELECT DISTINCT [Last Name], [First Name]
FROM [Level 4$]
WHERE City = [?])

PS Being queries, this approach will be efficient with large datasets.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top