View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
John[_19_] John[_19_] is offline
external usenet poster
 
Posts: 87
Default Can you sort one cell or a string in vba?

Thanks... I've been using somthing like that. i just thought vba would
have something more elegant
JOhn

Rick Rothstein wrote:
Here is my attempt at a solution. Place the following in a general
Module (Insert/Module from the VB editor's menu bar)...

Function SortCharacters(S As String) As String
Dim X As Long, Z As Long, Cnt As Long
ReDim C(1 To Len(S)) As Long
For X = 1 To Len(S)
For Z = 1 To Len(S)
If Mid(S, Z, 1) < Mid(S, X, 1) Then C(X) = C(X) + 1
Next
Next
SortCharacters = Space(Len(S))
For X = 1 To Len(S)
Mid(SortCharacters, C(X) + 1, 1) = Mid(S, X, 1)
Next
End Function

Just call this function from your own code passing the text you want to
sort. As an example...

MyString = "14386ah"
MsgBox SortCharacters(MyString)

This function can also be used as a UDF (user defined function) on the
worksheet as well. As an example...

=SortCharacters(A1)