Sort number characters within a cell

Captain Smith

Active Member
Joined
Feb 28, 2003
Messages
324
Let's say I have cells that each contain a number string, and the cells themselves are formatted as text:

358193
231454
12023
etc

I want to sort within each cell so the data looks like (must be text format because of the potential 0's at the beginning of the string):
133589
123445
01223

Number of characters per cell could differ. Solution either with formula or VBA would be fine.

As always, thanks in advance.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi

You can use this UDF:
Code:
Function OrderCell(sStr As String)

OrderCell = Join(Evaluate("transpose(if(row(1:" & Len(sStr) & "),small(--mid(""" & sStr & """,row(1:" & Len(sStr) & "),1),row(1:" & Len(sStr) & "))))"), "")
End Function

Use it as a formula. In B1:
=OrderCell(A1)

Hope this helps
PGC

Edit: Simplified the expression
Book1
ABCD
1358193133589
2231454123445
31202301223
4789021334789980234123001122233334477888999
598765432100123456789
69876543210987654321000112233445566778899
7987654321098765432109876543210000111222333444555666777888999
8
Sheet2
 
Upvote 0
Not very elegant but a formula solution which should work for strings containing any digits, including zeroes, but not more than 15 non-zero digits amongst them

=REPT(0,LEN(A1)-LEN(SUBSTITUTE(A1,0,"")))&SUM(10^(ROW(INDIRECT("1:"&LEN(SUBSTITUTE(A1,0,""))))-1)*LARGE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)+0,ROW(INDIRECT("1:"&LEN(SUBSTITUTE(A1,0,""))))))

confirmed with CTRL+SHIFT+ENTER

...or a formula using functions from Morefunc add-in which will work with any number of digits......

=MCONCAT(SMALL(MID(A1,INTVECTOR(LEN(A1),1),1)+0,INTVECTOR(LEN(A1),1)))

also confirmed with CTRL+SHIFT+ENTER
 
Upvote 0
Holy Cow - both solutions work great! Barry, I am especially impressed that you could do this with a formula. Elegance takes second place to a formula actually returning what it is supposed to!

Thanks very much both of you.
 
Upvote 0
By the way, the MoreFunc sort formula will return a greatest to least sort by changing SMALL to LARGE. This is not to say I understand entirely how the formula works (I don't). I just made an intuitive guess and it was correct. Even a blind hog finds an acorn once in a while.
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,370
Members
449,155
Latest member
ravioli44

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