Alphabetizing in word

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

ozo

Thread Starter
Joined
Jul 27, 2007
Messages
17
Hey guys, I need to alphabetize a certain list, but i cant figure out how to.
Example:
"K6YF ZBVF AKRH 9VLH GM87"

It's just a bunch of codes like that, no new lines inbetween or anything, just spaces, and i need to alphabetize them all. Any suggestions?

Thanks,
ozo
 
Joined
Sep 4, 2003
Messages
4,916
Here is some macro code I put together for you that will do what you want. The code assumes that there is only 1 space between each of the values. Just highlight the line of text you want to sort and run the macro. As always, make a copy of your orginal document before running any code on it.

Regards,
Rollin

Code:
Sub Alphabetize()

Dim stringArray() As String
Dim vSelection As String
stringArray = Split(Trim(Selection.Text), " ")

WordBasic.SortArray stringArray()

vSelection = stringArray(0) & " "

For i = 1 To UBound(stringArray)

vSelection = vSelection & stringArray(i) & " "

Next i

Selection.TypeText (vSelection)

    
End Sub

HERE IS ANOTHER WAY OF DOING IT
(This example be used in any VBA application unlike the code above which only works in MS Word)

Code:
Sub Alphabetize()

Dim stringArray() As String
Dim vSelection As String
stringArray = Split(Trim(Selection.Text), " ")

 For i = 0 To UBound(stringArray)
       For x = i To UBound(stringArray)
            If UCase(stringArray(x)) < UCase(stringArray(i)) Then
                str1 = stringArray(i)
                str2 = stringArray(x)
                stringArray(i) = str2
                stringArray(x) = str1
            End If
        Next x
 Next i
 
 vSelection = stringArray(0) & " "

For i = 1 To UBound(stringArray)

vSelection = vSelection & stringArray(i) & " "

Next i

Selection.TypeText (vSelection)
    
End Sub
 

ozo

Thread Starter
Joined
Jul 27, 2007
Messages
17
Stupid question... How would i go about running that macro?
Thanks very much for the time writing that!

And WhitPhil, that program does its job nicely, so thanks for that also, but I'm still interested to see how to work the macro.

Also, is there a way to find and erase dupes in word?

Thanks again guys!
 
Joined
Sep 4, 2003
Messages
4,916
Open Word and press ALT + F11 on your keyboard to bring up the VBA editor. Next click INSERT --> MODULE and copy and past the code into the blank module. Re-Save the Document and then highlight your line of text and run the macro called "Alphabetize" by clicking TOOLS --> MACRO --> MACROS and selecting the macro from the list.

To prevent duplicate values from being added just add an IF statement in the code like I've done below.

Code:
Sub Alphabetize()

Dim stringArray() As String
Dim vSelection As String
stringArray = Split(Trim(Selection.Text), " ")

WordBasic.SortArray stringArray()

vSelection = stringArray(0) & " "

For i = 1 To UBound(stringArray)

vTest = InStr(1, vSelection, stringArray(i) & " ")

If InStr(1, vSelection, stringArray(i) & " ") < 1 Then

vSelection = vSelection & stringArray(i) & " "

End If

Next i

Selection.TypeText (vSelection)

    
End Sub
Regards,
Rollin
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top