Sort Comma-Separated Index Page Numbers in Microsoft Word with VBA
Question details
Sort comma-separated page numbers in Word index entries numerically instead of correcting each entry by hand.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- An index contains many entries with page numbers in the wrong order, and some references may include note markers such as 18n3.
- Observed behavior
- An entry such as "neuroscience, 13, 40, 27" needs to be rewritten with its page numbers in ascending order. Note references can cause a basic numeric sorting macro to fail.
Use a desktop version of Word that supports VBA, and select only the index paragraphs you want to process. The macro below handles entries with a label followed by at least two comma-separated whole page numbers; it skips ranges, note references, and labels containing commas.
Sort simple index entries with a Word VBA macro
Use this macro to sort whole-number references in selected paragraphs and skip entries that need special handling.
The macro expects each paragraph to look like "neuroscience, 13, 40, 27". It leaves entries containing references such as 18n3 or 15–17 unchanged, rather than guessing how they should be sorted.
Replacing paragraph text can remove character-level formatting within processed entries. If the index is generated by a Word INDEX field, updating that field may overwrite manual or macro changes.
In desktop Word, select Developer > Visual Basic, then choose Insert > Module. If the Developer tab is hidden, enable it in Word's ribbon settings.
Paste this code into the module: Sub SortSelectedIndexPages() Dim p As Paragraph, r As Range Dim parts As Variant, nums() As Long Dim i As Long, j As Long, temp As Long Dim token As String, result As String Dim valid As Boolean, changed As Long, skipped As Long For Each p In Selection.Paragraphs Set r = p.Range.Duplicate Do While r.End > r.Start token = Right$(r.Text, 1) If token = Chr$(13) Or token = Chr$(7) Then r.End = r.End - 1 Else Exit Do End If Loop parts = Split(r.Text, ",") valid = UBound(parts) >= 2 If valid Then valid = Len(Trim$(parts(0))) > 0 If valid Then ReDim nums(1 To UBound(parts)) For i = 1 To UBound(parts) token = Trim$(parts(i)) If Len(token) = 0 Or Len(token) > 9 Or token Like "*[!0-9]*" Then valid = False Exit For End If nums(i) = CLng(token) Next i End If If valid Then For i = 1 To UBound(nums) - 1 For j = i + 1 To UBound(nums) If nums(j) < nums(i) Then temp = nums(i) nums(i) = nums(j) nums(j) = temp End If Next j Next i result = Trim$(parts(0)) For i = 1 To UBound(nums) result = result & ", " & CStr(nums(i)) Next i r.Text = result changed = changed + 1 Else skipped = skipped + 1 End If Next p MsgBox changed & " entries processed; " & skipped & " paragraphs skipped." End Sub
Return to the document and select the index paragraphs. Choose Developer > Macros, select SortSelectedIndexPages, and click Run.
Check the results in the selected index. Manually review entries with note markers, page ranges, labels containing commas, or other nonnumeric references.

Correct individual index entries manually
Use manual editing for a small index or for entries with special references that the macro skips.
Edit and review Word index documents with WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing documents. Its Microsoft Word format compatibility makes it easy to move documents between suites, though it does not fix Word's VBA macro behavior.

Frequently Asked Questions
Will the macro sort 13, 40, 27 as numbers rather than text?
Yes. For a paragraph such as "neuroscience, 13, 40, 27", the macro converts the references to numbers and rewrites them as "neuroscience, 13, 27, 40".
Can the macro sort references such as 18n3?
No. It skips the entire paragraph when a reference contains a note marker. To sort such references automatically, extend the parser and decide how notes should be ordered relative to ordinary page numbers.
Why did the macro skip an index entry?
The macro requires a label followed by at least two comma-separated whole numbers. It skips entries with page ranges, note references, extra commas in the label, empty references, or numbers longer than nine digits.
Will updating a generated Word index keep the sorted results?
Not necessarily. Updating an INDEX field can regenerate its contents and overwrite edits. Review the underlying index entries before updating the field.
Where can I run the macro in Word?
Select the index paragraphs in desktop Word, then choose Developer > Macros, select SortSelectedIndexPages, and click Run. The macro must first be added to a VBA module.




