Sort by smallest to largest, multi lines, one cell

mduntley

Board Regular
Joined
May 23, 2015
Messages
139
Office Version
  1. 365
Platform
  1. Windows
I am wondering if its possible to have a VBA to convert a cell with multipule lines? I have a file that has 500+ cell that have multi-lines, so i am looking to have a costume vba where i can = to a cell. I have put an example what I want

[TABLE="class: grid, width: 128"]
<tbody>[TR]
[TD="width: 64"]Cell one[/TD]
[TD="width: 64"]1
6
8
7
11
23
456
4
12
13
465
251
631[/TD]
[/TR]
</tbody>[/TABLE]

We can see that in B1 the number is not sorted. I am wondering if there is a VBA formula to make it in order as the next example

[TABLE="class: grid, width: 128"]
<tbody>[TR]
[TD="width: 64"]Cell one[/TD]
[TD="width: 64"]1
4
6
7
8
11
12
13
23
251
456
465
631[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You could try a user-defined function like this one. It assumes that the items on each line in the cell are numerical.
To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.

Code:
Function SortCell(s As String) As String
  Dim itm As Variant
  Dim AL As Object

  Set AL = CreateObject("System.Collections.ArrayList")
  For Each itm In Split(s, vbLf)
    AL.Add Val(itm)
  Next itm
  AL.Sort
  SortCell = Join(AL.ToArray(), vbLf)
End Function

Excel Workbook
AB
116871123456412134652516311467811121323251456465631
25656
3
4110168711112345641213465251631467812132325145646563111011111
Sort
 
Upvote 0
You could try a user-defined function like this one. It assumes that the items on each line in the cell are numerical.
To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.

Code:
Function SortCell(s As String) As String
  Dim itm As Variant
  Dim AL As Object

  Set AL = CreateObject("System.Collections.ArrayList")
  For Each itm In Split(s, vbLf)
    AL.Add Val(itm)
  Next itm
  AL.Sort
  SortCell = Join(AL.ToArray(), vbLf)
End Function


Is there a way to make this code be available to live in another document and it will work in any new document?
 
Upvote 0
Is there a way to make this code be available to live in another document and it will work in any new document?
Yes, move the function into your 'Personal.xlsb' workbook. However, you would then have to use the function like this
=PERSONAL.XLSB!SortCell(A1)

If you want to avoid having to prefix with PERSONAL.XLSB! each time, or add a reference to Personal.xlsb for each new workbook, you would need to create an Add-In. More information here and here or Google..
 
Upvote 0
My apologies to post on an old message, but I like this concept. How do I do this with text?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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