+ Reply to Thread
Results 1 to 6 of 6

How to order according to comma and bracket?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    How to order according to comma and bracket?

    The VBA below can order according to comma:
    Sub jOrder()
       
    Dim Ray As Variant
    Dim Temp As String
    Dim I As Integer
    Dim j As Integer
        
    Ray = Split(ActiveCell, ",")
    
    For I = 0 To UBound(Ray)
       For j = I To UBound(Ray)
          If Ray(j) < Ray(I) Then
             Temp = Ray(I)
             Ray(I) = Ray(j)
               Ray(j) = Temp
            End If
        Next j
    Next I
    ActiveCell.Value = Join(Ray, ",")
    
    End Sub
    a,c,e,b,d
    to
    a,b,c,d,e

    How to revise VBA so that it can order within bracket:

    a,c(b,c,a),e,b,d
    to
    a,b,c(a,b,c),d,e

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to order according to comma and bracket?

    Will the bracket set always stay with the preceding letter; "c" in your example?

    Can there be more than one bracket set?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to order according to comma and bracket?

    The bracket will be set with any letter or word and more than one bracket set:

    a,c,boy and girl(C-2,c-3,c-1),e,d(2,5,4)
    to
    a,boy and girl(c-1,C-2,c-3),c,d(2,4,5),e

    The aim is to order the words within bracket first;
    and then order according to comma.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: How to order according to comma and bracket?

    Here is one way.

    Sub Test()
    
        Dim Text As String
        Dim startBracket As Long
        Dim endBracket As Long
        Dim index As Long
        Dim useChr As String
        Dim delimiter As String
        
        Text = ActiveCell.Text
        delimiter = ","
        
        useChr = ""
        For index = 128 To 255
            If InStr(Text, Chr(index)) = 0 Then
                useChr = Chr(index)
                Exit For
            End If
        Next
        
        If Len(useChr) = 0 Then
            MsgBox "Can not find unique temporary character. Text not sorted", vbExclamation
            Exit Sub
        End If
        
        endBracket = 1
        startBracket = InStr(endBracket, Text, "(")
        Do While startBracket > 0
            endBracket = InStr(startBracket, Text, ")")
            Mid(Text, startBracket + 1, endBracket - startBracket - 1) = Replace(SortText(Mid(Text, startBracket + 1, endBracket - startBracket - 1), delimiter, xlAscending, vbTextCompare), delimiter, useChr)
            startBracket = InStr(endBracket, Text, "(")
        Loop
        
        Text = Replace(SortText(Text, delimiter, xlAscending, vbTextCompare), useChr, delimiter)
        ActiveCell.Offset(0, 1) = Text
        
    End Sub
    
    Function SortText(Text As String, Optional delimiter As String = ",", Optional SortOrder As XlSortOrder, Optional CompareMethod As VbCompareMethod = VbCompareMethod.vbBinaryCompare) As String
       
        Dim items As Variant
        Dim tempItem As Variant
        Dim returnValue As String
        Dim outerIndex As Long
        Dim innerIndex As Long
        Dim lowerArraySize As Long
        Dim upperArraySize As Long
        Dim arrayStep As Long
    
        items = Split(Text, delimiter)
        If SortOrder = xlAscending Then
            lowerArraySize = LBound(items)
            upperArraySize = UBound(items)
            arrayStep = 1
        Else
            lowerArraySize = UBound(items)
            upperArraySize = LBound(items)
            arrayStep = -1
        End If
        
        For outerIndex = lowerArraySize To upperArraySize Step arrayStep
            For innerIndex = outerIndex To upperArraySize Step arrayStep
                If StrComp(items(innerIndex), items(outerIndex), CompareMethod) < 0 Then
                    tempItem = items(outerIndex)
                    items(outerIndex) = items(innerIndex)
                    items(innerIndex) = tempItem
                End If
            Next
        Next
    
        returnValue = Join(items, delimiter)
        SortText = returnValue
        
    End Function
    In your examples you are not doing a case sensitive check. The function has parameters to handle sort order and type of comparison.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,627

    Re: How to order according to comma and bracket?

    Try this code

    Sub SplitAndJoin()

    Dim IpStr As String
    Dim BktNos As Long, Bkt As Long, BktRem As Long
    Dim TA As Long, TB As Long
    
    IpStr = Range("A3")
    
    BktNos = Len(IpStr) - Len(Replace(IpStr, "(", ""))
    ReDim Str(BktNos) As String
    
    For Bkt = 1 To BktNos
    
        If InStr(1, IpStr, "(") > 0 Then
        Str(Bkt) = Mid(Left(IpStr, InStr(1, IpStr, ")") - 1), InStr(1, IpStr, "(") + 1)
        IpStr = Replace(IpStr, "(" & Str(Bkt) & ")", "ZZZ" & Bkt)
        
        M = Split(Str(Bkt), ",")
                For TA = 0 To UBound(M) - 1
                    For TB = TA + 1 To UBound(M)
                    
                    If UCase(M(TA)) > UCase(M(TB)) Then
                    temp = M(TA)
                    M(TA) = M(TB)
                    M(TB) = temp
                    temp = ""
                    End If
                    
                    Next TB
                Next TA
        Str(Bkt) = "(" & Join(M, ",") & ")"
        End If
    
    Next Bkt
    
        M = Split(IpStr, ",")
                For TA = 0 To UBound(M) - 1
                    For TB = TA + 1 To UBound(M)
                    
                    If UCase(M(TA)) > UCase(M(TB)) Then
                    temp = M(TA)
                    M(TA) = M(TB)
                    M(TB) = temp
                    temp = ""
                    End If
                    
                    Next TB
                Next TA
        IpStr = Join(M, ",")
    
    For BktRem = 1 To BktNos
    
    IpStr = Replace(IpStr, "ZZZ" & BktRem, Str(BktRem))
    
    Next BktRem
    
    Range("B3") = IpStr
    
    End Sub
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  6. #6
    Forum Contributor
    Join Date
    09-18-2011
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    375

    Re: How to order according to comma and bracket?

    Thanks Andy Pope and kvsrinivasamurthy, both of yours are works.

    I have revised kvsrinivasamurthy's VBA so that it can apply to any cells (refer '<<<):


    Sub SplitAndJoin()
    Dim IpStr As String
    Dim BktNos As Long, Bkt As Long, BktRem As Long
    Dim TA As Long, TB As Long
    
    IpStr = ActiveCell.Value  '<<<
    BktNos = Len(IpStr) - Len(Replace(IpStr, "(", ""))
    ReDim Str(BktNos) As String
    
    For Bkt = 1 To BktNos
    
        If InStr(1, IpStr, "(") > 0 Then
        Str(Bkt) = Mid(Left(IpStr, InStr(1, IpStr, ")") - 1), InStr(1, IpStr, "(") + 1)
        IpStr = Replace(IpStr, "(" & Str(Bkt) & ")", "ZZZ" & Bkt)
        
        M = Split(Str(Bkt), ",")
                For TA = 0 To UBound(M) - 1
                    For TB = TA + 1 To UBound(M)
                    
                    If UCase(M(TA)) > UCase(M(TB)) Then
                    temp = M(TA)
                    M(TA) = M(TB)
                    M(TB) = temp
                    temp = ""
                    End If
                    
                    Next TB
                Next TA
        Str(Bkt) = "(" & Join(M, ",") & ")"
        End If
    
    Next Bkt
    
        M = Split(IpStr, ",")
                For TA = 0 To UBound(M) - 1
                    For TB = TA + 1 To UBound(M)
                    
                    If UCase(M(TA)) > UCase(M(TB)) Then
                    temp = M(TA)
                    M(TA) = M(TB)
                    M(TB) = temp
                    temp = ""
                    End If
                    
                    Next TB
                Next TA
        IpStr = Join(M, ",")
    
    For BktRem = 1 To BktNos
    
    IpStr = Replace(IpStr, "ZZZ" & BktRem, Str(BktRem))
    
    Next BktRem
    
    ActiveCell.Value = IpStr '<<<
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sort comma separated string, but in specific order
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-05-2017, 08:53 AM
  2. [SOLVED] VBA to categorize text with comma, semicolon and bracket
    By sroysroy in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-23-2015, 10:53 PM
  3. [SOLVED] Custom Order Sort with Comma(,) as Criteria
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 04:37 AM
  4. Replies: 2
    Last Post: 07-18-2014, 03:00 AM
  5. [SOLVED] Order / sort range by number in bracket first then remainder alphabetically.
    By skyping in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2013, 08:14 AM
  6. Help with math bracket calculation order
    By ronlaboa in forum Excel General
    Replies: 3
    Last Post: 12-12-2012, 04:58 AM
  7. Remove String Within the Bracket and also the Bracket
    By seanyeap in forum Excel General
    Replies: 6
    Last Post: 04-23-2010, 10:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1