0

I have a string that looks like

"Carol, Ernie, Alice, Bob, Dave"

I want to pass it to a VBA function that sorts it so it becomes

"Alice, Bob, Carol, Dave, Ernie"

Is there a smart way to do this with built in functions that doesn't require a lot of custom code or am I out of luck?

braX
  • 11,506
  • 5
  • 20
  • 33
user525966
  • 469
  • 1
  • 6
  • 15
  • use split then load that array into an [arraylist](https://excelmacromastery.com/vba-arraylist/) sort it and then use join to put it back or loop the array list to concatenate it back. – Scott Craner Sep 06 '22 at 23:15
  • I was afraid of that, out of luck I suppose – user525966 Sep 06 '22 at 23:16

1 Answers1

1

Using the idea at https://stackoverflow.com/a/45379461/2193968

This should work:

Function SortCSV(Value As String)
    Set Arr = CreateObject("System.Collections.ArrayList")
    For Each Item In Split(Value, ",")
        Arr.Add Trim(Item)
    Next
    Arr.Sort
    SortCSV = Join(Arr.ToArray, ",")
    Set Arr = Nothing
End Function
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32