1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Alphabetizing in word

Discussion in 'Business Applications' started by ozo, Oct 26, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. ozo

    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
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    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
     
  3. WhitPhil

    WhitPhil Gone but never forgotten Trusted Advisor

    Joined:
    Oct 4, 2000
    Messages:
    8,684
  4. ozo

    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!
     
  5. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    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
     
  6. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/644037

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice