Array Formula - sort results alphabetically

kenderweasel

New Member
Joined
Feb 17, 2017
Messages
40
Hi,

I have an array formula, which returns a list of text codes - at the moment, these are not sorted alphabetically, however, I've now been asked to change this to return the codes in alphabetical order. Can anyone help?

=IFERROR(INDEX(Database!$B$1:$DD$1,1,SMALL(IF(((Database!$C$1:$DD$1000=$A$16)+(Database!$C$1:$DD$1000=$A$17)+(Database!$C$1:$DD$1000=$A$18))*(Database!A$1:A$1000=$H$7),COLUMN(Database!$C$1:$DD$1000)),ROW(A1))-(ROW(A$2)-1),1),"")
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Man, kenderweasel, you really drove me crazy! :)

I looked at your question, and my first thought was it's possible. Then as I deconstructed your formula, I decided it's definitely NOT possible! But it kept nagging at me all day, and I thought of some ways to get around the problem, only to have another one arise, then I'd figure that one out, and so on. So here's what I came up with:

BCDE
ListListListList
xxxxcccc
ccccpppp
ppppxxxx

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

</tbody>
Sheet3

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Array Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]B2[/TH]
[TD="align: left"]{=IFERROR(INDEX(Database!$B$1:$I$1,1,SMALL(IF(((Database!$C$1:$I$10=$A$16)+(Database!$C$1:$I$10=$A$17)+(Database!$C$1:$I$10=$A$18))*(Database!A$1:A$10=$H$7),COLUMN(Database!$C$1:$I$10)),ROW(A1))-(ROW(A$2)-1),1),"")}[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]C2[/TH]
[TD="align: left"]{=IFERROR(INDEX(Database!$C$1:$I$1,SMALL(IF(Database!A$1:A$10=$H$7,IF(ISNUMBER(MATCH(Database!$C$1:$I$10,$A$16:$A$18,0)),COLUMN(Database!$C$1:$I$10)-COLUMN(Database!$C$1)+1)),ROWS($C$2:$C2))),"")}[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]D2[/TH]
[TD="align: left"]{=IFERROR(INDEX(Database!$B$1:$I$1,MATCH(0,MMULT(COLUMN(Database!$B$1:$I$1)^0,IF(IF(MMULT(TRANSPOSE(ROW(Database!$A$1:$A$10)),(Database!$A$1:$A$10=$H$7)*(COUNTIF($D$1:$D1,Database!$B$1:$I$1)=0)*((Database!$B$1:$I$10=$A$16)+(Database!$B$1:$I$10=$A$17)+(Database!$B$1:$I$10=$A$18))),Database!$B$1:$I$1,"a")>TRANSPOSE(IF(MMULT(TRANSPOSE(ROW(Database!$A$1:$A$10)),(Database!$A$1:$A$10=$H$7)*(COUNTIF($D$1:$D1,Database!$B$1:$I$1)=0)*((Database!$B$1:$I$10=$A$16)+(Database!$B$1:$I$10=$A$17)+(Database!$B$1:$I$10=$A$18))),Database!$B$1:$I$1,"a")),1,0))-SUM(IF(MMULT(TRANSPOSE(ROW(Database!$A$1:$A$10)),(Database!$A$1:$A$10=$H$7)*(COUNTIF($D$1:$D1,Database!$B$1:$I$1)=0)*((Database!$B$1:$I$10=$A$16)+(Database!$B$1:$I$10=$A$17)+(Database!$B$1:$I$10=$A$18)))=0,1)),0)),"")}[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E2[/TH]
[TD="align: left"]{=IFERROR(INDEX($B$2:$B$10,MATCH(0,IF($B$2:$B$10="",-1,COUNTIF($B$2:$B$10,"<"&$B$2:$B$10))-ROWS($E$2:$E2)+1-SUM(IF($B$2:$B$10="",1)),0)),"")}[/TD]
[/TR]
</tbody>[/TABLE]
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself[/TD]
[/TR]
</tbody>[/TABLE]



The first formula in B2 is your exact formula. The one in C2 does exactly the same thing, but a bit more concisely written. It helped to figure out exactly what yours was doing. Then the petite formula in D2 extracts the same values in sorted order. It's not extensively tested since I don't have your data to work with. Also note that I tested on a much smaller range than your original formula. If you want to try it, you'll have to change the ranges to match your sheet. HOWEVER, this formula is VERY computationally intensive, and I shudder to think about how it will slow down your sheet.

You'd probably be better off using your current formula in B2 as a helper column, then something like the formula in E2 to sort them.

So give me an interesting problem, and it'll nag at me until I solve it, but I spent a lot of time today working on a formula that will probably not be usable. If you give it a try, let me know how it works.
 
Upvote 0
Thank you so much for taking so much time to get a solution :)

I've ended up using E2 in combination with my original formula, as I couldn't get D2 to work - but it does do what I need, so it's great :D

Thanks :)
Anj
 
Upvote 0
That's probably the best idea. The D2 formula is proof that it can be done, but it's inefficient and probably needs tweaking to make it work with your sheet. But I am happy you got the E2 formula to work for you, that was almost an afterthought when I added that.

Glad to help! :cool:
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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