+ Reply to Thread
Results 1 to 3 of 3

Sort a string in VBA

Hybrid View

  1. #1
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Sort a string in VBA

    Hi all,

    I have a string in VBA like the following:

    "A/12/3,C/4/18,D/14/2,B/23/1,A/13/4" and that goes on for a while
    Is there a way to sort the string so it would read:
    "A/12/3,A/13/4,B/23/1,C/4/18,D/14/2"

    The example is a very condensed version of reality, so I can't do it manually.
    Does anyone have any bright ideas or suggestions?

    Thanks,

    Jasper

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Sort a string in VBA

    There is a long loop called Bubble sort code which has been around for long time. You should be easily find it if you goggle it, or it might be on C Pearson's site

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Sort a string in VBA

    Hi Jasper, long time no speak, hope you're well

    Sub quickTest()
    
        Const test = "A/12/3,C/4/18,D/14/2,B/23/1,A/13/4"
        Dim g
        g = Split(test, ",")
        QuickSort g, LBound(g), UBound(g)
        
        Debug.Print Join(g, ",")
        
    End Sub
    
    Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
    
      Dim pivot   As Variant
      Dim tmpSwap As Variant
      Dim tmpLow  As Long
      Dim tmpHi   As Long
    
      tmpLow = inLow
      tmpHi = inHi
    
      pivot = vArray((inLow + inHi) \ 2)
    
      While (tmpLow <= tmpHi)
    
         While (vArray(tmpLow) < pivot And tmpLow < inHi)
            tmpLow = tmpLow + 1
         Wend
    
         While (pivot < vArray(tmpHi) And tmpHi > inLow)
            tmpHi = tmpHi - 1
         Wend
    
         If (tmpLow <= tmpHi) Then
            tmpSwap = vArray(tmpLow)
            vArray(tmpLow) = vArray(tmpHi)
            vArray(tmpHi) = tmpSwap
            tmpLow = tmpLow + 1
            tmpHi = tmpHi - 1
         End If
    
      Wend
    
      If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
      If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
    
    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. [SOLVED] split and sort a string
    By se3unlock in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-18-2015, 04:38 AM
  2. [SOLVED] String-like based Sort
    By AnushPatel in forum Excel General
    Replies: 6
    Last Post: 10-30-2014, 08:40 AM
  3. VBA sort string of numbers
    By dubbdan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-20-2013, 02:08 AM
  4. How to sort chains and add a string?
    By salaso69 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-26-2013, 01:33 AM
  5. Sort by part of a string
    By Motox in forum Excel General
    Replies: 3
    Last Post: 10-09-2012, 11:26 AM
  6. Sort by a portion of a String
    By Stupidav in forum Excel General
    Replies: 3
    Last Post: 10-07-2010, 04:50 PM
  7. building a sort string
    By Andrew Hicks in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-24-2007, 08:02 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