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
 
I'm sos sorry man It did not work.... : (

I tought it was going to be really easy.... I'm giving up with this..... I can't figure it out..... I can send you my excel file with some data and you can see and example.

Please try to help me with this ok : )

Thank you so much
 
Upvote 0

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
Maybe you can shorten some of that code a little bit, starting with routines shortened as such...



<font face=Tahoma New>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> insertFormattedData(wks <SPAN style="color:#00007F">As</SPAN> Worksheet, lngCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>)
    <SPAN style="color:#007F00">'Insert formatted data back to Excel file</SPAN>
    wks.Range("C" & lngCount).Value = bookCode
    wks.Range("T" & lngCount).Value = series
    wks.Range("U" & lngCount).Value = body
    wks.Range("AB" & lngCount).Value = cntr
    wks.Range("AL" & lngCount).Value = div
    wks.Range("AM" & lngCount).Value = defer
    wks.Range("AN" & lngCount).Value = alliance
    wks.Range("AK" & lngCount).Value = gmrpo
    wks.Range("I" & lngCount).Value = group
    wks.Range("V" & lngCount).Value = qty
    wks.Range("AO" & lngCount).Value = spl
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>



Then you can just call like this ...



<font face=Tahoma New><SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> main()
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#007F00">'MODIFY THIS - FIRST ROW TO BE PROCESSED</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#007F00">'MODIFY THIS - LAST ROW TO BE PROCESSED</SPAN>
    i = 4
    j = 7000
    count = i
    <SPAN style="color:#00007F">Set</SPAN> ws = Worksheets("Original")
    ws.Cells.NumberFormat = "@"
    <SPAN style="color:#00007F">Call</SPAN> setServiceYear(ws)
    ws.Columns("W").Replace What:="H", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows
    ws.Columns("I").Replace What:=".", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows
    ws.Columns("T").Copy Destination:=ws.Range("AJ1")
    cell = "m" & count
    partnum = ws.Range(cell).Value
    <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">While</SPAN> Trim(partnum) <> "" <SPAN style="color:#00007F">Or</SPAN> count = j + 1
        <SPAN style="color:#00007F">Call</SPAN> getGMDATdata(ws, count)
        <SPAN style="color:#00007F">Call</SPAN> formatGroup <SPAN style="color:#007F00">'Format group (no .)</SPAN>
        <SPAN style="color:#00007F">Call</SPAN> formatQty <SPAN style="color:#007F00">'Format quantity to 2 digits</SPAN>
        <SPAN style="color:#00007F">Call</SPAN> convertBookSeriesBody <SPAN style="color:#007F00">'Format book code, series, and body style</SPAN>
        <SPAN style="color:#00007F">Call</SPAN> insertFormattedData(ws, count) <SPAN style="color:#007F00">'Insert formatted data back into Excel</SPAN>
        ws.Rows(count & ":" & count).Interior.ColorIndex = 2
        count = count + 1
        cell = "m" & count
        partnum = ws.Range(cell).Value
    <SPAN style="color:#00007F">Loop</SPAN>
    MsgBox "Format Complete", vbInformation
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>



That would give you a couple of different procedures ...


<font face=Tahoma New>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> getGMDATdata(wks <SPAN style="color:#00007F">As</SPAN> Worksheet, lngCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>)
    ctlgType = Trim(wks.Range("D" & lngCount).Value) <SPAN style="color:#007F00">'Catalogue Type</SPAN>
    bookCode = Trim(wks.Range("C" & lngCount).Value) <SPAN style="color:#007F00">'Book Code</SPAN>
    series = Trim(wks.Range("T" & lngCount).Value) <SPAN style="color:#007F00">'Series</SPAN>
    body = Trim(wks.Range("U" & lngCount).Value) <SPAN style="color:#007F00">'Body Style</SPAN>
    gmrpo = Trim(wks.Range("AF" & lngCount).Value) <SPAN style="color:#007F00">'GM RPO</SPAN>
    drive = Trim(wks.Range("AE" & lngCount).Value) <SPAN style="color:#007F00">'Left or Right Drive</SPAN>
    group = Trim(wks.Range("I" & lngCount).Value) <SPAN style="color:#007F00">'Group</SPAN>
    qty = Trim(wks.Range("V" & lngCount).Value) <SPAN style="color:#007F00">'Quantity</SPAN>
    cntr = ""
    defer = ""
    div = ""
    alliance = ""
    spl = ""
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>


.. and ..


<font face=Tahoma New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> setServiceYear(wks <SPAN style="color:#00007F">As</SPAN> Worksheet)
    <SPAN style="color:#00007F">With</SPAN> wks.Columns("P:Q")
        .Replace What:="98", Replacement:="2004", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="01", Replacement:="2004", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="02", Replacement:="2004", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="03", Replacement:="2004", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="04", Replacement:="2004", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="05", Replacement:="2005", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="06", Replacement:="2006", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="07", Replacement:="2007", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="08", Replacement:="2008", LookAt:=xlWhole, SearchOrder:=xlByRows
        .Replace What:="09", Replacement:="2009", LookAt:=xlWhole, SearchOrder:=xlByRows
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


And when you call your procedures, just change them as such ...



<font face=Tahoma New>                <SPAN style="color:#00007F">Call</SPAN> getnorUSChevyRTConversionOptions
                <SPAN style="color:#00007F">Call</SPAN> getRTDivCntrSeriesBody("1", "UCM")
                <SPAN style="color:#00007F">Call</SPAN> insertrowtest(6, count)
                <SPAN style="color:#00007F">Call</SPAN> insertFormattedData(ws, count)</FONT>


Just be sure to Dim the ws variable in each sub; no need to Dim it if it's in the header of the module declared for the entire module.


Also, no need for the paren's, such as ...


<font face=Tahoma New>    <SPAN style="color:#00007F">If</SPAN> ((InStr(series, "ULS") <> 0) And (InStr(series, "ULT") <> 0) And (InStr(series, "US") <> 0)) <SPAN style="color:#00007F">Then</SPAN></FONT>


.. can become ..


<font face=Tahoma New>    <SPAN style="color:#00007F">If</SPAN> InStr(series, "ULS") <> 0 And InStr(series, "ULT") <> 0 And InStr(series, "US") <> 0 <SPAN style="color:#00007F">Then</SPAN></FONT>
 
Upvote 0
Also, here is an example of how to use the function with a specified string...



<font face=Tahoma New><SPAN style="color:#00007F">Function</SPAN> mySortCell(body <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">Optional</SPAN> Delimiter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> arrTmp() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, vElm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Delimiter = "" <SPAN style="color:#00007F">Then</SPAN> Delimiter = ", "
    arrTmp = Split(body, Delimiter)
    <SPAN style="color:#00007F">For</SPAN> i = <SPAN style="color:#00007F">LBound</SPAN>(arrTmp) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(arrTmp)
        <SPAN style="color:#00007F">For</SPAN> j = i + 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(arrTmp)
            <SPAN style="color:#00007F">If</SPAN> Val(arrTmp(i)) > Val(arrTmp(j)) <SPAN style="color:#00007F">Then</SPAN>
                vElm = arrTmp(j)
                arrTmp(j) = arrTmp(i)
                arrTmp(i) = vElm
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>
    mySortCell = Join(arrTmp, Delimiter)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> TestThisPlease()
    MsgBox mySortCell("3, 5, 4, 2, 1")
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>



You can use the principle of passing variables between subs the same way with functions.

HTH
 
Upvote 0
Sorry guys.....is not working..... I think I'm going to give up with this.....But first I just want to try something different for last time, but I need your help.

I was thinking to used the fisrt code that firefytr posted here. My idea is add a new code in my macro to create a new column next to column "U" column "V" and used =sortcell(U4,",") all the way down. After that, delete the original column "U" and keep the sort data in column "V". If we delete column "U" column "V" will be Column "U", and with this I don't have to doit manualy.


Can I do something like that? and can you help me to create the macro?
Thank you guys...I think this is going to be the last try.
SG
 
Upvote 0
Why not manually?

Enter the formula in the top-most row, copy down. Select all formula cells in that column, copy it, then Edit | Paste Special | Values, then delete the other column.
 
Upvote 0
OK THANK YOU SO MUCH..

I THINK I WILL DO IT MANUALLY

I HAVE ANOTHER QUESTION FOR YOU GUYS AND I KNOW YOU CAN HELP ME : ) ... BUT I WILL POST IT LATER...

THANK YOU GUYS FOR EVERYTHING
SG
 
Upvote 0

Forum statistics

Threads
1,215,586
Messages
6,125,689
Members
449,250
Latest member
azur3

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