How to Use VBA to Mark Correct Quiz Answers in Word
Question details
The user wants to use a VBA macro to automatically process a quiz document, mark the correct answers with an asterisk, and delete the lines containing the answer keys.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Formatting a text-based quiz or test document where correct answers are listed at the bottom of each question block in an 'ANS: ' format.
- Observed behavior
- Currently, the document displays the correct answers in a separate line below the choices. It requires manual formatting to place an asterisk next to the correct options and manually delete the answer key lines.
Before running any macro that deletes or alters document content, save a backup copy of your quiz file, as VBA macro executions generally cannot be undone using the standard Undo shortcut.
Use a Bottom-Up VBA Macro to Mark Answers and Delete ANS Lines
Processing the document from the end to the beginning ensures that deleting the ANS lines does not disrupt the paragraph indexing for the rest of the document.
When writing a macro to delete paragraphs in Word, processing from top to bottom causes paragraph index numbers to shift, which can cause the macro to skip lines or throw errors. Working bottom-up prevents deleted lines from changing the positions of the content that still needs to be processed.
The logic of the macro should involve finding the 'ANS:' identifier, extracting the correct letters, navigating upwards to locate matching answer paragraphs, and modifying them before destroying the 'ANS:' paragraph.
In your word processor, navigate to the Developer tab and click on 'Visual Basic', or press ALT + F11 on your keyboard to open the VBA Editor.
In the VBA Editor, right-click on your document project in the left pane, select 'Insert', and then click 'Module'.
Write your VBA loop to iterate backwards. Use a structure like `For i = ActiveDocument.Paragraphs.Count To 1 Step -1` to safely handle paragraph deletions.
Inside the loop, check if the current paragraph starts with 'ANS:'. If it does, extract the correct answer letters using string manipulation functions like `Split`.
Program the macro to search upward from the current paragraph for the matching answer choice letters. Prepend an asterisk (*) to the text of the matching paragraphs, and finally use `ActiveDocument.Paragraphs(i).Range.Delete` to remove the ANS line.

Automate Document Formatting with WPS Writer
WPS Writer supports advanced document processing and VBA macros (in supported editions), allowing you to quickly process complex quiz documents, mark answers, and automate repetitive formatting tasks with ease.
- 1. Open Your Quiz Document: Launch WPS Office and open your .doc or .docm quiz document in WPS Writer.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If it is not enabled, you can activate it in the WPS Options menu.
- 3. Launch the VBA Editor: Click on 'VBA Editor' or press ALT + F11 to launch the macro programming environment.
- 4. Execute Your Code: Paste your bottom-up quiz processing VBA code into a new module and click 'Run' to instantly mark all correct answers.

Frequently Asked Questions
Why must the macro process the document from the bottom up?
When you delete a paragraph in a document, all subsequent paragraphs shift up, changing their index numbers. Processing from top to bottom would cause the macro loop to skip lines or crash. Working backward (from the end to the beginning) ensures that deleting an ANS line only affects paragraphs that have already been processed.
How can the macro handle questions with multiple correct answers?
If an ANS line contains multiple answers (e.g., 'ANS: A, C'), the VBA macro can use the `Split` function to separate the letters by the comma delimiter. It can then iterate through this array of letters, searching upward to find and mark each corresponding choice paragraph.
What if some questions have 3 choices and others have 5?
A robust macro does not move a fixed number of lines upward. Instead, it dynamically searches the text of the preceding paragraphs for the specific choice letters extracted from the ANS line. This allows it to adapt perfectly to questions with any number of answer choices.
Why am I getting a 'Type Mismatch' error when running the macro?
This usually happens if the macro encounters an unexpected format, such as an ANS line with missing data or an empty paragraph being improperly processed. Ensure your code verifies that the paragraph actually contains text before attempting string manipulation.




