How to Alphabetize Comma-Separated Names Using a Word VBA Macro
Question details
The user wants to sort a selected, comma-separated list of names or places in alphabetical order using a Word VBA macro.

- Product
- Word Processing Software
- Device & OS
- not provided
- Scenario
- Organizing inline text documents where lists of names or items are written continuously and separated by commas, requiring alphabetical sorting without manual retyping.
- Observed behavior
- A selected inline list of comma-separated items needs to be split, sorted alphabetically, and rebuilt automatically with the proper final conjunction.
Ensure that the Developer tab is enabled in your word processor's ribbon, and remember to strictly highlight the comma-separated text string before executing the macro.
Create and Run a VBA Macro to Sort the Selected List
Use a custom VBA macro to split the selected text, sort the items, and rejoin them into a properly formatted list.
This solution utilizes standard VBA functions to manipulate string data directly within your document. It effectively replaces manual sorting by splitting the text into an array, alphabetizing it, and outputting the finished sentence.
In your document, press Alt + F11 to launch the Visual Basic for Applications editor.
Click 'Insert' > 'Module' and write a script that takes the current selection. Use the Split function with a comma delimiter, like Split(selectionText, ", "), to turn the list into an array.
Use the WordBasic.SortArray a() function to quickly alphabetize the items, which is much faster than writing nested sorting loops manually.
Use the Join function to rebuild the text string. Apply a string replacement function to restore the final conjunction (e.g., replacing the last comma with ', and').
Return to your document, select the comma-separated names, navigate to the Developer tab, click 'Macros', and run your newly created sorting macro.

Modify the Macro for Semicolons and Embedded Conjunctions
Adjust the VBA script's delimiters and replacement logic if your list uses semicolons or if the items themselves contain the word 'and'.
Sort Lists Quickly and Run Macros in WPS Writer
WPS Writer offers an advanced, highly compatible VBA environment (available in premium versions) that allows you to effortlessly execute custom macros, automate formatting, and organize your text directly within the application.
- 1. Enable the Developer Tab: Open WPS Writer, navigate to the settings, and ensure the Developer tools are enabled to access the VBA editor.
- 2. Add the Sorting Script: Click 'Macros', create a new module, and paste your array sorting and string splitting VBA code.
- 3. Select Text and Execute: Highlight your comma-separated list in the document, return to the Macros menu, and click 'Run' to instantly alphabetize your names.

Frequently Asked Questions
Why did the macro mess up names that contain the word 'and'?
If your macro is programmed to automatically replace the final comma with 'and', it might conflict with list items that already include the word. To resolve this, remove the automatic conjunction replacement logic from your VBA script and type the final 'and' manually.
Can I use this macro to sort lists separated by characters other than commas?
Yes. You simply need to change the delimiter used in the macro's Split function. For example, change Split(s, ", ") to Split(s, "; ") to properly handle semicolon-separated lists.
Is there a simpler way to sort the array within the VBA script without using nested loops?
Yes, you can utilize the built-in WordBasic.SortArray a() function in your VBA code. This function efficiently alphabetizes the array without requiring you to manually script complex nested sorting loops.




