Consulting

Results 1 to 12 of 12

Thread: Sorting numbers in a single cell

  1. #1
    VBAX Newbie
    Joined
    Dec 2004
    Posts
    1
    Location

    Sorting numbers in a single cell

    Is there a way to sort a list of numbers in a single cell, from least to greatest?

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Cronje, and welcome to the board!

    Are you numbers truly in a single cell, or in multiple cells but in a single column or row?

    If they are in a single cell, how are they separated? By commas, hard returns (Alt Enter), or something else?

    For the latter, it's fairly easy, but if in the former, it would be a bit more complicated.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi Cronje, welcome to VBAX!


    If you are talking about having some numbers in a cell, and they are seperated by spaces, then you can use something like this, it goes into a standard module ...


    [vba]Option Explicit

    Sub SortMyCellPlease()
    Dim myCel As Range, tmpWs As Worksheet, tmpCel As Range
    Dim strTmpVal As String, i As Long
    Set myCel = Selection
    If myCel.Count > 1 Then
    MsgBox "Please, only one cell at a time!", vbInformation, "ERROR"
    Exit Sub
    End If
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set tmpWs = Worksheets.Add
    Set tmpCel = tmpWs.Range("A1")
    tmpCel.Value = myCel.Value
    tmpCel.TextToColumns Destination:=tmpCel, DataType:=xlDelimited, Space:=True
    tmpCel.EntireRow.Sort Key1:=tmpCel, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
    DataOption1:=xlSortNormal
    For i = 1 To tmpWs.Range("IV1").End(xlToLeft).Column Step 1
    strTmpVal = strTmpVal & tmpWs.Cells(1, i).Value & " "
    Next i
    myCel.Value = Trim(strTmpVal)
    tmpWs.Delete
    Application.ScreenUpdating = True
    Application.DisplayAlerts = False
    End Sub[/vba]

    HTH

  4. #4
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    It does not work for me. I get an error here: Dataoption1:=xlSortNormal

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    What version of Excel are you running?

    Anyway, you never did answer Ken's question. First things first, can you post an example spreadsheet (zipped)? This will help greatly as so we can 'see' what you are looking at.

  6. #6
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    It was not my post I just tryed because I was courious.

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Doh! Oops, sorry.

    But if you post an example sheet, we couuld see what you mean.

  8. #8
    VBAX Regular
    Joined
    Jun 2004
    Location
    Cordova, Alaska
    Posts
    10
    Location
    Man, Zack. You're too much. (SortMyCellPlease)? :rofl

    I've been know to name mine things like DoIt

    Hope you & your's have an excellent holiday season.
    Dan

  9. #9
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Dataoption1:=xlSortNormal is only available from xl2002 (10) onwards, I assume you ran it in 2000 gsouza. Just remove that bit and it will run fine.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  10. #10
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Okay I will try it, thank you, yes I am running 2000

  11. #11
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Cool it worked perfectly. Thanks again

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by mark007
    Dataoption1:=xlSortNormal is only available from xl2002 (10) onwards, I assume you ran it in 2000 gsouza. Just remove that bit and it will run fine.

    Exactly why I asked the version. Compiled on 2002.

    Dan: I aim to please!

Posting Permissions

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