Sort text in Cell

alexoigres

Board Regular
Joined
Dec 2, 2005
Messages
184
Hello,

I want to sort some values inside a cell

Example:

A2
19,48,35,21

What I want is

A2
19,21,35,48

I have a new file every week and I want to create a macro to do this.

Any ideas??
thank you
 
Allways post your code!
Or you just get a general answers.

How experienced are you at writing your own code?

I am assuming that you want the code to sort the data cell and replace that data with sorted data in that same cell?
 
Upvote 0

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
Yes.... I want to do that.
I want the code to sort the data cell and replace that data with sorted data in that same cell : )


..... and for the other question I don't have any experience to create a Macro... I can modify one... and make it works for me. About copy and paste my macro ... my macro it's to big but I can send you the file by email and you can check the code....the person how wrote the macro was good in programming.

So...Do you want me to send you the excel file? or you already know what i want?

Let me know you email adress if you want me to send the file to you.

: )
You are great... thank you so much for everything man
SG
 
Upvote 0
What I need to know is what holds the cell address to be sorted, is it the ActiveCell, the Selection or is it a Range of cells, what in your code is the name of this cell reference?
 
Upvote 0
The code runs by selecting the data cell and running the Sub: mySortCell


Private Function Array_Sort(ByVal NotSortedArry As Variant) As Variant
Dim i As Long, j As Long, vElm As Variant

For i = LBound(NotSortedArry) To UBound(NotSortedArry)
For j = i + 1 To UBound(NotSortedArry)

If Val(NotSortedArry(i)) > Val(NotSortedArry(j)) Then
vElm = NotSortedArry(j)
NotSortedArry(j) = NotSortedArry(i)
NotSortedArry(i) = vElm
End If

Next
Next

Array_Sort = NotSortedArry
End Function

Sub mySortCell()
Dim arr() As String, arrTmp() As String
Dim myVal As Variant
Dim i As Long

myVal = ActiveCell.Value
If myVal = "" Then Exit Sub

Delimiter = ", "
arr = Array_Sort(Split(myVal, Delimiter))

ActiveCell.Value = Join(arr, Delimiter)
End Sub
 
Upvote 0
THIS IS MY CODE

This is my cell that I want to sort......

cell = "u" & count
body = Trim(Range(cell).Value) 'Body Style

I hope you can help me to add what i want to this Code....

THANK YOU SO MUCH

************************
 
Upvote 0
Copy this to where you have your code, then every where you have finished working on: "body", just below it add:

mySortCell

to run the sort code:

Private Function Array_Sort(ByVal NotSortedArry As Variant) As Variant
Dim i As Long, j As Long, vElm As Variant

For i = LBound(NotSortedArry) To UBound(NotSortedArry)
For j = i + 1 To UBound(NotSortedArry)

If Val(NotSortedArry(i)) > Val(NotSortedArry(j)) Then
vElm = NotSortedArry(j)
NotSortedArry(j) = NotSortedArry(i)
NotSortedArry(i) = vElm
End If

Next
Next

Array_Sort = NotSortedArry
End Function

Sub mySortCell()
Dim arr() As String, arrTmp() As String
Dim i As Long

If body = "" Then Exit Sub

Delimiter = ", "
arr = Array_Sort(Split(body, Delimiter))

body = Join(arr, Delimiter)
End Sub


and it should work?
 
Upvote 0
I copied and pasted you code at the end of my code.....
then at the end of every part with "body" i added

mySortCell

but when i tried to run the macro i had this error in the sub mySortCell()

compile error
Variable not defined

in this part.....

Delimiter = ", "
arr = Array_Sort(Split(body, Delimiter))

I got confuse ......can you help me to resolve the problem

Thank you so much.....

SG
 
Upvote 0
I did it... but nothing happend... I run my Macro MAIN.... and then the Macro mySortCell and nothing happend.....

What do you think I'm doing wrong?

Is to much for you to add your macro to my macro and then just paste it here.....could you do it for me ... please....

Thank you for you patience
 
Upvote 0
Looking at your code I do not see where "body" is placed in a cell?

For the code to wotk your code needs to have a location for the replaced value.

You may only need to put "mySortCell" after the Loop at the end of the Main code?

Like:

Loop

mySortCell

'You may need something here, like: ActiveCell.Value = body
'But, I cannot tell, you use Selection at the top of your code,
'which may work as well. The Copy Series to a different location
'may make this type of range selection not return the correct
'location for the cell update?

msgbox ("Format Complete")
End Sub


But, you still need code to place the sorted value, unless I am missing something in your code. I cannot test it, you have too much custom information in the code to test it efficently.
 
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,942
Members
449,275
Latest member
jacob_mcbride

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