+ Reply to Thread
Results 1 to 20 of 20

VBA: How to auto sort a series in a single cell

  1. #1
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    VBA: How to auto sort a series in a single cell

    I am looking for a way to automatically sort a series of comma-separated numbers in a single cell. I am using MS Excel.

    In the attached spreadsheet I'm wanting to sort the numbers in Sheet 2, cells B2 and C2 into ascending numerical order.

    Screenshot 2019-10-25 at 15.25.37.png

    Thanks in advance.

    chrubble
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,880

    Re: How to auto sort a series in a single cell

    Can't be done without VBA - shall I move the thread for you?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    Re: How to auto sort a series in a single cell

    Yes, please. The spreadsheet that I attached to the post already has some VBA in it, also.

    Thank you!

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this ‼


    According to your attachment as a beginner starter :

    PHP Code: 
    Sub Demo1()
          Const 
    ","
            
    Dim VC%, WN&
        
    With [B2:C2]
                
    = .Value2
            
    For 1 To .Count
                With 
    New Collection
                    
    For Each W In Split(V(1C), S)
                             
    Val(W)
                         For 
    1 To .Count
                            
    If .Item(N) > W Then .Add W, , N0: Exit For
                         
    Next
                            
    If N Then .Add W
                    Next
                        V
    (1C) = .Item(1)
                        For 
    2 To .Count:  V(1C) = V(1C) & & .Item(N):  Next
                End With
            Next
               
    .Value2 V
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Another way …


    According to your attachment a starter demonstration via Excel basics formula :

    PHP Code: 
    Sub Demo2()
      Const 
    ","
        
    Dim VC&
      
    With [B2.C2]
            
    = .Value2
        
    For 1 To .Count
            V
    (1C) = Join(Evaluate("IF({1},SMALL({" V(1C) & "},COLUMN(" & [A1].Resize(, UBound(Split(V(1C), S)) + 1).Address ")))"), S)
        
    Next
           
    .Value2 V
      End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,411

    Re: VBA: How to auto sort a series in a single cell

    Here is one more method that you can consider...
    Please Login or Register  to view this content.

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,411

    Re: Another way …

    Quote Originally Posted by Marc L View Post
    Please Login or Register  to view this content.
    For Marc (or anyone else who might know)...

    I have been using Evaluate for years now and I don't think I have come across, or at least noticed, this before. I was trying to simplify the above line of code and kept erroring out, so I decided to strip it down to the bare essentials with the idea of building it up from there. That is when I got stumped. In its barest form, your formula becomes this...
    Please Login or Register  to view this content.
    The resulting text string argument fed to the Evaluate function is identical to the resulting text string argument that your code feeds to the Evaluate function; however, your code runs and the one I wrote generates a "Type Mismatch" error? Just to reiterate, when I put a breakpoint on that line of code and execute these two statements (the text string argument for the Evaluate function)...

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    they both print out the identical text string. So my question... why does the Evaluate function care how its text string argument is constructed?

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Hi Rick !


    Quote Originally Posted by Rick Rothstein View Post
    Here is one more method that you can consider...
    Nope ‼ chrubble can't consider your Windows ArrayList on his MAC version ! The reason why I started with a VBA Collection …


    Quote Originally Posted by Rick Rothstein View Post
    they both print out the identical text string. So my question... why does the Evaluate function care how its text string argument is constructed?
    Nope again as the Evaluate method does not care about the string construction but just about its value !
    So your bad as you forgot to check out for the second cell, C2 !
    Just using Debug.Print you will see your formula is not the same as mine …

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    A formula variation :

    PHP Code: 
    Sub Demo2a()
      Const 
    "IF({1},SMALL({¤},COLUMN(A:#)))"","
        
    Dim VC&
      
    With [B2.C2]
            
    = .Value2
        
    For 1 To .Count
            V
    (1C) = Join(Evaluate(Replace (Replace(F"¤"V(1C)), "#"Chr(UBound(Split(V(1C), S)) + 65))), S)
        
    Next
           
    .Value2 V
      End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  10. #10
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    Re: Another way …

    Hi Marc L - Thanks for the response . How do I add this into VBA? As a new module? Sorry, I'm a complete novice with VBA. Currently my VBA project looks like this...
    Screenshot 2019-10-28 at 10.44.46.png
    All the best,
    chrubble

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Directly in the worksheet module …

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,880

    Re: VBA: How to auto sort a series in a single cell

    Marc L - the OP has said they are a novice. Please explain HOW to get something "directly in the worksheet module". Thanks.

  13. #13
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    Re: VBA: How to auto sort a series in a single cell

    Hi Mark L,

    Thanks for your helpful comment.

    Can you explain to me how to add your PHP code "directly in the worksheet module"?

    Many thanks,

    chrubble

  14. #14
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA: How to auto sort a series in a single cell

    I'd suggest you insert a new module (or use the existing one) and just copy and paste it in.
    Rory

  15. #15
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    Re: VBA: How to auto sort a series in a single cell

    Quote Originally Posted by rorya View Post
    I'd suggest you insert a new module (or use the existing one) and just copy and paste it in.
    Thanks Rory.

    The structure of my VBA Project is posted above - Which directory or folder should I create the module in? The 'Modules' sub-directory of the 'VBAProject (Sample_1294080_Mac.xlsb)' project?

    Many thanks,

    chrubble

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by chrubble View Post
    Can you explain to me how to add your PHP code "directly in the worksheet module"?
    Two ways to paste my VBA code :

    • On VBE side double click on the worksheet name in the Project window of your workbook

    • Right click on the worksheet tab name then choose the code option …

  17. #17
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA: How to auto sort a series in a single cell

    Yes, or you could simply add it to the existing Module1.

  18. #18
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    Re: VBA: How to auto sort a series in a single cell

    Thanks Rory and Mark,

    I added the code as a new module (Module 2) under the 'VBAProject (Sample_1294080_Mac.xlsb)' project. But when I try to run it, I get a run-time '424' (Object required) error. The debug shows that the issue is the 'V = .Value2' line in the code.

    Attachment 648414

    Any suggestions?

    I've attached the updated version of the file.

    Many thanks,

    chrubble
    Attached Files Attached Files

  19. #19
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA: How to auto sort a series in a single cell

    Try this version:

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    10-24-2019
    Location
    Sheffield, England
    MS-Off Ver
    MS Excel for Mac (16.29.1)
    Posts
    11

    Re: VBA: How to auto sort a series in a single cell

    Perfect - That worked. THANK YOU!

+ 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. Single cell maximum drawdown from price series
    By DH123 in forum Excel General
    Replies: 2
    Last Post: 05-31-2016, 04:37 AM
  2. Defining series values range from a single cell
    By stegngas in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-29-2014, 12:36 PM
  3. [SOLVED] summing a series of numbers in a single cell
    By eddiebautista in forum Excel General
    Replies: 3
    Last Post: 10-28-2013, 03:17 AM
  4. Dynamic Data Validation based on series in a single cell
    By ExcelLefty in forum Excel General
    Replies: 4
    Last Post: 09-29-2009, 08:45 AM
  5. How to tally series of 1's in a single cell?
    By sproutus in forum Excel General
    Replies: 1
    Last Post: 11-29-2007, 05:00 PM
  6. Displaying single and multiple data series.Single data series
    By Donald Macleod in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-29-2006, 11:00 PM
  7. [SOLVED] chart data series -- plot a table as a single series
    By hjc in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 09-20-2005, 01:05 PM

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