Accending Order

Karan001

Board Regular
Joined
Jul 22, 2009
Messages
113
Hi Experts,
I have a column where there are so many value in rows.My requirement is to arrange all the row value in accending orders.

Available Value :

<TABLE style="WIDTH: 313pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=417><COLGROUP><COL style="WIDTH: 313pt; mso-width-source: userset; mso-width-alt: 15250" width=417><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 313pt; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20 width=417>8Weeks/1Week/4Weeks/1Year/2Years/26Weeks/12Weeks</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20>4Weeks/1Week/5Years/12Weeks/1.5 Year</TD></TR></TBODY></TABLE>

Requirement :-

<TABLE style="WIDTH: 339pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=452><COLGROUP><COL style="WIDTH: 339pt; mso-width-source: userset; mso-width-alt: 16530" width=452><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 339pt; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl66 height=20 width=452>1Week/4Weeks/8Weeks/12Weeks/26Weeks/1Year/2Years</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 339pt; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20 width=452>1Week/4Weeks/12Weeks/1.5 Year/5Years</TD></TR></TBODY></TABLE>


Regards,
Karan
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try;
Code:
Public Sub SortRows()
    Dim rngRow As Range
    Dim rngTable As Range
    
    Set rngTable = [B][COLOR="Red"]Range("A1:H20")[/COLOR][/B]'Change this bit
    
    For Each rngRow In rngTable.Rows
        rngRow.Sort Key1:=rngRow.Resize(1, 1), Order1:=xlAscending, Orientation:=xlSortRows, Header:=xlNo
    Next rngRow
End Sub
 
Upvote 0
Hi Hayden,
I tried your code but not working can you check it.Because when i running macro no change happened.For your information both the value is in A1 & A2 Cell respectively


Regards,
Karan
 
Upvote 0
Do you mean that the entire string "8Weeks/1Week/4Weeks/1Year/2Years/26Weeks/12Weeks" is contained in a single cell?
 
Upvote 0
Hi,

I think this is the continuation of this thread

try

Code:
Function KARAN(ByRef r As Range) As String
Dim x, i As Long, d As Object, w, y
Dim PosW As Long, PosY As Long
Set d = CreateObject("scripting.dictionary")
    d.comparemode = 1
With CreateObject("scripting.dictionary")
    .comparemode = 1
    x = Split(r.Value, "/")
    For i = 0 To UBound(x)
        PosW = InStr(1, x(i), "week", 1)
        If PosW Then
            .Item(Format(Left(x(i), PosW - 1), "00") & Mid$(x(i), PosW)) = Empty
        Else
            PosY = InStr(1, x(i), "year", 1)
            d.Item(Format(Left(x(i), PosY - 1), "00") & Mid$(x(i), PosY)) = Empty
        End If
    Next
    If .Count Then w = Join$(SortCell(.keys), "/")
    If d.Count Then
        KARAN = w & "/" & Join$(SortCell(d.keys), "/")
    Else
        KARAN = w
    End If
End With
End Function
Function SortCell(ByRef v)
    Dim i   As Long, j As Long, t
    For i = LBound(v) To UBound(v)
        For j = i To UBound(v)
            If v(j) < v(i) Then
                t = v(i)
                v(i) = v(j)
                v(j) = t
            End If
        Next
    Next
    SortCell = v
End Function
 
Upvote 0
Hi,

I think this is the continuation of this thread
Kris

1. The previous thread was about removing duplicates from the cells whereas this one is about sorting the data in the the cells so I think it is okay to be a separate thread.

2. For me, your suggestion here doesn't quite work with the given data (and a few extra rows I have added).

Excel Workbook
AB
28Weeks/1Week/4Weeks/1Year/2Years/26Weeks/12Weeks01Week/04Weeks/08Weeks/12Weeks/26Weeks/01Year/02Years
34Weeks/1Week/5Years/12Weeks/1.5 Year01Week/04Weeks/12Weeks/02Year/05Years
43 Years/03Years
52Weeks02Weeks
64Weeks/105.5Week/5Years/12Weeks/1.5 Year04Weeks/106Week/12Weeks/02Year/05Years
Arrange Text in Order (3)



Karan

My suggestion actually replaces the original data so definitely should be tested in a copy of your workbook. If you need help with how to modify to suit a different layout or to put this somewhere other than over the original data, post back with more details.

Before code:

Excel Workbook
A
1
28Weeks/1Week/4Weeks/1Year/2Years/26Weeks/12Weeks
34Weeks/1Week/5Years/12Weeks/1.5 Year
43 Years
52Weeks
64Weeks/105.5Week/5Years/12Weeks/1.5 Year
Arrange Text in Order (1)




Code:

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Order_List()<br>    <SPAN style="color:#00007F">Dim</SPAN> DataRange <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> Data, TempData, t<br>    <SPAN style="color:#00007F">Dim</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>, L <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, M <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> RX1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, RX2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> RX1 = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">With</SPAN> RX1<br>        .Pattern = "(.+?)(?=[A-Z ])"<br>        .Global = <SPAN style="color:#00007F">False</SPAN><br>        .IgnoreCase = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> RX2 = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">With</SPAN> RX2<br>        .Pattern = "[A-Z]"<br>        .Global = <SPAN style="color:#00007F">False</SPAN><br>        .IgnoreCase = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> DataRange = Range("A2", Range("A" & Rows.Count).End(xlUp))<br>    Data = DataRange.Value<br>    L = <SPAN style="color:#00007F">UBound</SPAN>(Data, 1)<br>    Columns("B:C").Insert<br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> L<br>        s = Data(i, 1)<br>        <SPAN style="color:#00007F">If</SPAN> InStr(1, s, "/") > 0 <SPAN style="color:#00007F">Then</SPAN><br>            t = Split(s, "/")<br>            M = <SPAN style="color:#00007F">UBound</SPAN>(t)<br>            <SPAN style="color:#00007F">ReDim</SPAN> TempData(1 <SPAN style="color:#00007F">To</SPAN> M + 1, 1 <SPAN style="color:#00007F">To</SPAN> 3)<br>            <SPAN style="color:#00007F">For</SPAN> j = 0 <SPAN style="color:#00007F">To</SPAN> M<br>                TempData(j + 1, 1) = RX2.Execute(t(j))(0)<br>                TempData(j + 1, 2) = RX1.Execute(t(j))(0)<br>                TempData(j + 1, 3) = t(j)<br>            <SPAN style="color:#00007F">Next</SPAN> j<br>            <SPAN style="color:#00007F">With</SPAN> Range("B1:D" & M + 1)<br>                .Value = TempData<br>                .Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 2), _<br>                    Order2:=xlAscending, Header:=xlNo<br>                Data(i, 1) = Join(Application.Transpose(.Offset(, 2).Resize(, 1).Value), "/")<br>                .ClearContents<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    Columns("B:D").Delete<br>    DataRange.Value = Data<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>



After code:

Excel Workbook
A
1
21Week/4Weeks/8Weeks/12Weeks/26Weeks/1Year/2Years
31Week/4Weeks/12Weeks/1.5 Year/5Years
43 Years
52Weeks
64Weeks/12Weeks/105.5Week/1.5 Year/5Years
Arrange Text in Order (2)
 
Last edited:
Upvote 0
Another option !!!

Code:
[COLOR="Navy"]Sub[/COLOR] MG17May16
[COLOR="Navy"]Dim[/COLOR] RStg
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] w [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] y [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] j [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] tx [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ray
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & rows.Count).End(xlUp))
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        w = 0
         y = 0
          RStg = Split(Dn, "/")
           [COLOR="Navy"]If[/COLOR] UBound(RStg) > 0 [COLOR="Navy"]Then[/COLOR]
             ReDim wk(1 To UBound(RStg))
              ReDim Yr(1 To UBound(RStg))
    [COLOR="Navy"]For[/COLOR] n = 0 To UBound(RStg)
        [COLOR="Navy"]If[/COLOR] InStr(RStg(n), "Week") [COLOR="Navy"]Then[/COLOR]
            w = w + 1
            wk(w) = Trim(Split(RStg(n), "W")(0))
        [COLOR="Navy"]ElseIf[/COLOR] InStr(RStg(n), "Year") [COLOR="Navy"]Then[/COLOR]
            y = y + 1
            Yr(y) = Trim(Split(RStg(n), "Y")(0))
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
ReDim Preserve wk(1 To w)
  ReDim Preserve Yr(1 To y)
    Ray = Array(wk, Yr)
    [COLOR="Navy"]For[/COLOR] n = 0 To UBound(Ray)
        [COLOR="Navy"]For[/COLOR] i = 1 To UBound(Ray(n))
            [COLOR="Navy"]For[/COLOR] j = i To UBound(Ray(n))
                [COLOR="Navy"]If[/COLOR] Val(Ray(n)(j)) < Val(Ray(n)(i)) [COLOR="Navy"]Then[/COLOR]
                    Temp = Ray(n)(i)
                    Ray(n)(i) = Ray(n)(j)
                    Ray(n)(j) = Temp
                [COLOR="Navy"]End[/COLOR] If
            [COLOR="Navy"]Next[/COLOR] j
        [COLOR="Navy"]Next[/COLOR] i
    [COLOR="Navy"]For[/COLOR] tx = 1 To UBound(Ray(n))
        [COLOR="Navy"]If[/COLOR] n = 0 [COLOR="Navy"]Then[/COLOR]
            Ray(n)(tx) = Ray(n)(tx) & "Week"
        [COLOR="Navy"]Else[/COLOR]
            Ray(n)(tx) = Ray(n)(tx) & "Year"
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] tx
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]With[/COLOR] Application
    Dn = Join(.Transpose(.Transpose(Ray(0))), "/") & "/" & Join(.Transpose(.Transpose(Ray(1))), "/")
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi,

OK. Thanks Peter for checking the code. Here is the revised one.

Code:
Function KARAN(ByRef r As Range) As String
Dim x, i As Long, d As Object, w, y
Dim PosW As Long, PosY As Long
Set d = CreateObject("scripting.dictionary")
    d.comparemode = 1
With CreateObject("scripting.dictionary")
    .comparemode = 1
    x = Split(r.Value, "/")
    For i = 0 To UBound(x)
        If InStr(1, x(i), "week", 1) Then
            .Item(x(i)) = Empty
        Else
            d.Item(x(i)) = Empty
        End If
    Next
    If .Count Then
        w = Join$(SortCell(.keys, True), "/")
        If d.Count Then
            KARAN = w & "/" & Join$(SortCell(d.keys, False), "/")
        Else
            KARAN = w
        End If
    ElseIf d.Count Then
        KARAN = Join$(SortCell(d.keys, False), "/")
    End If
End With
End Function
Function SortCell(ByRef v, ByVal WeekItIs As Boolean)
    Dim i   As Long, j As Long, t
    Dim x1, x2
    For i = LBound(v) To UBound(v)
        For j = i To UBound(v)
            If WeekItIs Then
                x1 = Replace(Replace(LCase$(v(j)), "weeks", ""), "week", "")
                x2 = Replace(Replace(LCase$(v(i)), "weeks", ""), "week", "")
            Else
                x1 = Replace(Replace(LCase$(v(j)), "years", ""), "year", "")
                x2 = Replace(Replace(LCase$(v(i)), "years", ""), "year", "")
            End If
            If CSng(x1) < CSng(x2) Then
                t = v(i)
                v(i) = v(j)
                v(j) = t
            End If
        Next
    Next
    SortCell = v
End Function
 
Upvote 0
Hi Peter,Mick & Krishna,
Really excellent codes given by you all. I checked all the codes & all are working.

Thanks to all of you for giving your continuous support.

Regards
Karan
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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