Consulting

Results 1 to 2 of 2

Thread: How can sort Ascending for data in a Cell

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    How can sort Ascending for data in a Cell

    I have a cell (A1) and i have also some data like (1-4-3-dog-5-2-12-40-50-cat-)
    How can i sort by Ascending for a data on cell with macro ?

    Thank you .

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    [vba]
    Sub Sort_Cell_Contents()
    'ADOPTED FROM:
    'http://www.ozgrid.com/forum/showthread.php?t=78972

    Dim cll As Range, rng As Range
    Dim delim As String

    Set rng = Range("A2:G100") 'change to actual range
    delim = "-" 'change to actual delimiter

    Application.EnableEvents = False
    For Each cll In rng
    With cll
    .Value = Trim(.Value)
    If Right(.Value, 1) = delim Then
    .Value = Left(.Value, Len(.Value) - 1)
    End If
    If InStr(.Value, delim) > 0 Then
    x = Split(.Value, delim)
    SortA x, 0, UBound(x)
    .Value = Join(x, delim)
    End If
    End With
    Next
    Application.EnableEvents = True

    End Sub




    Private Sub SortA(ary, LB, UB)

    Dim M As Variant, temp
    Dim i As Long, ii As Long

    i = UB: ii = LB
    M = ary(Int((LB + UB) / 2))

    Do While ii <= i
    Do While Val(ary(ii)) < Val(M)
    ii = ii + 1
    Loop
    Do While Val(ary(i)) > Val(M)
    i = i - 1
    Loop
    If ii <= i Then
    temp = ary(ii): ary(ii) = ary(i): ary(i) = temp
    ii = ii + 1: i = i - 1
    End If
    Loop

    If LB < i Then SortA ary, LB, i
    If ii < UB Then SortA ary, ii, UB

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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