INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Add Spaces to Alphabetized String

Add Spaces to Alphabetized String

(OP)
I found code for a function that will alphabetize the characters in a string.

If A2 is "TACO" then Sortletters(A2) returns "ACOT." What I'd like the result to look like is "A C O T" with a space in between the characters. A leading space before the first character is okay - I'm just looking for readability.

Here is the code:

Function SortLetters(v As Variant) As String
Dim bFlag As Boolean
Dim n As Long, i As Long
Dim s As String

n = Len(v)
If n = 0 Then Exit Function

ReDim arrS(0 To Len(v))
For i = 1 To Len(v)
arrS(i) = Mid$(v, i, 1)
Next

Do
bFlag = True
For i = LBound(arrS) To UBound(arrS) - 1
If arrS(i) > arrS(i + 1) Then
bFlag = False
arrS(0) = arrS(i)
arrS(i) = arrS(i + 1)
arrS(i + 1) = arrS(0)
End If
Next i
Loop While Not bFlag

s = ““
For i = 1 To UBound(arrS)
s = s & arrS(i)
Next

SortLetters = s

End Function

What would I add to get the spacing I want?

Thanks,

GS

******^*******
I HateAmbiguity.

RE: Add Spaces to Alphabetized String

HI,

CODE

For i = 1 To UBound(arrS)
s = s & arrS(i) & " "
Next
s = Left(s, Len(s) - 1) 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add Spaces to Alphabetized String

(OP)

Awesomesauce! That works - thank you!

******^*******
I HateAmbiguity.

RE: Add Spaces to Alphabetized String

...or you could just do:

CODE

s = Join(arrS, " ")
s = Left(s, Len(s) - 1) 

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Add Spaces to Alphabetized String

Andy, never used Join() before. Thx!

Actually you only need the one statement...

CODE

s = Join(arrS, " ") 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add Spaces to Alphabetized String

"never used Join() before" you could have knocked me over with a feather smile
I 'll be walking the whole day with my nose up - I knew something Skip did not know (that's the first wink )

Once I've discovered Split() and Join() functions, I don't know how I lived without them before...

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Add Spaces to Alphabetized String

Ah, the mystique of a persona. Having occupied nearly three quarters of a century, I still have an awful lot to learn!

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add Spaces to Alphabetized String

Just for fun, here's an version that leverages the .NET core library:

CODE

Public Function SortLetters(v As String) As String
    Dim char As Variant
    Dim mystring() As Byte

    mystring = StrConv(v, vbFromUnicode)
    With CreateObject("system.collections.arraylist")
        For Each char In mystring
            .Add Chr$(char)
        Next
        .Sort
        SortLetters = Join(.toarray(), " ")
    End With
End Function 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close