VBA & Macro Problems

How to Add Sequential Numbers to Questions Using Word VBA Macro

Algirdas JasaitisAlgirdas Jasaitis Oct 28, 2026 868 views

Question details

The user wants to automate the insertion of sequential superscript numbers before existing question numbers in a long Word document using a VBA macro.

How to Add Sequential Numbers to Questions Using Word VBA Macro
Product
Word
Device & OS
not provided
Scenario
Formatting a long questionnaire or exam document where sequential numbering needs to be added programmatically.
Observed behavior
The user needs a VBA script to find paragraph-leading numbers and prepend an incrementing superscript number to each match.
Before you start

Before running any VBA macro, ensure you save a backup copy of your document to prevent accidental data loss or formatting errors.

Solution 1Recommended

Use a VBA Macro to Insert Sequential Superscript Numbers

This method uses a custom VBA script to find paragraph-leading numbers and automatically insert an incrementing superscript number before each one.

The macro utilizes Word's Find feature with wildcards to locate numbers at the beginning of paragraphs. Once found, it inserts a formatted sequential number right before the existing text.

1
Open the Visual Basic Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

In the VBA Editor, click on 'Insert' in the top menu and select 'Module' to create a new standard module.

3
Paste the VBA Code

Copy and paste the following code into the module window: Dim i As Long Dim rng As Range Selection.HomeKey wdStory i = 1 With Selection.Find Do While .Execute(FindText:="^13[0-9]{1,}", Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=True) Set rng = Selection.Paragraphs(2).Range Selection.Collapse wdCollapseEnd rng.Collapse wdCollapseStart rng.Text = "(" & i & ") " rng.Font.Superscript = True i = i + 1 Loop End With

4
Run the Macro

Close the VBA Editor. Press Alt + F8 to open the Macros dialog box, select your newly created macro, and click 'Run'.

Use a VBA Macro to Insert Sequential Superscript Numbers
Adjusting the Search Pattern: Test the macro on a short sample first. If your question numbers use a different format, you may need to adjust the wildcard search pattern (^13[0-9]{1,}) in the code to match your specific layout.
Advanced Document Formatting with WPS Office

Automate Document Formatting with WPS Writer

WPS Office provides robust support for VBA macros, allowing you to automate repetitive tasks like sequential numbering just as you would in Microsoft Word.

  1. 1. Open your document in WPS Writer: Launch WPS Writer and open the document you wish to format.
  2. 2. Access the Developer Tab: Navigate to the 'Tools' tab and click on 'Developer' to access the macro features.
  3. 3. Open the VBA Editor: Click 'Macros' or press Alt + F11 to open the WPS VBA Editor.
  4. 4. Run your Macro: Insert a new module, paste your VBA code, and run it to format your document instantly.
Fully compatible with Microsoft Word (.doc, .docx) formatsSupports VBA macros for advanced document automationLightweight and fast performance for long documentsFree to download with a familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why is the VBA macro not finding my question numbers?

The macro uses a specific wildcard pattern (`^13[0-9]{1,}`) to find numbers at the beginning of a paragraph. If your questions start with letters, Roman numerals, or have spaces before the number, you will need to modify the `FindText` pattern in the code to match your document's specific format.

How do I enable macros if they are disabled?

To run macros, you may need to adjust your security settings. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select 'Enable all macros'. Remember to revert this setting to a safer option after you finish running your trusted macro.

Can I change the formatting of the inserted numbers?

Yes. In the VBA code, the line `rng.Font.Superscript = True` makes the inserted number superscript. You can change this to `rng.Font.Bold = True` for bold text, or modify the `rng.Text = "(" & i & ") "` line to change the brackets or spacing around the number.