How to Create an Excel VBA Quiz That Closes the Workbook on a Wrong Answer
Question details
The user wants to build an automated Excel quiz that triggers immediately upon opening the file, evaluates the user's input, closes the workbook if the answer is wrong, and displays an image if the answer is correct.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Building an interactive Excel-based quiz or access-control gateway utilizing VBA macros and UserForms.
- Observed behavior
- Requires custom VBA scripting using workbook open events and conditional logic to enforce validation and control application behavior.
Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and that you have enabled the Developer tab in your ribbon to access the Visual Basic Editor.
Use a VBA UserForm and Workbook Open Event
Create a custom UserForm to collect the quiz answer and trigger it using the Workbook_Open event to validate the response as soon as the file is accessed.
This method utilizes a macro that launches automatically when the workbook opens. It presents a UserForm where the user types their answer. If the answer matches the predefined correct string, an image is revealed. If the answer is incorrect, the VBA code forces the workbook to close without saving, effectively denying further access.
Press 'Alt + F11' on your keyboard to open the Visual Basic for Applications (VBA) editor.
Click 'Insert' > 'UserForm' from the top menu. From the Toolbox, add a 'TextBox' (for the user's answer), a 'CommandButton' (to submit), and an 'Image' control (for the success picture). Select the Image control and set its 'Visible' property to False in the Properties window.
Double-click the CommandButton you added to open its code window. Enter the following logic: If TextBox1.Text = "YourCorrectAnswer" Then Image1.Visible = True Else ThisWorkbook.Close SaveChanges:=False
In the Project Explorer pane on the left, double-click 'ThisWorkbook'. Choose 'Workbook' from the left dropdown and 'Open' from the right dropdown. Enter this code: UserForm1.Show
Close the VBA editor. Go to 'File' > 'Save As', and ensure you select 'Excel Macro-Enabled Workbook (*.xlsm)' from the file format dropdown so your code is preserved.

Create and Run VBA Macros Seamlessly in WPS Spreadsheet
WPS Office provides robust support for VBA macros, allowing you to create custom UserForms, automate complex logic, and build interactive spreadsheets without switching applications.
- 1. Enable the Developer Tools: Open WPS Spreadsheet, go to the top ribbon, and click on the 'Developer' tab.
- 2. Launch the VBA Editor: Click the 'VBA Editor' icon to open the Visual Basic environment.
- 3. Build Your Quiz: Insert a UserForm and add your validation logic exactly as you would in standard Excel environments.
- 4. Save Your Work: Save your file in the .xlsm format to ensure full cross-compatibility and preserve your VBA project.

Frequently Asked Questions
Why doesn't the workbook close when I type the wrong answer?
This usually happens if macros are disabled on the computer opening the file. The 'Workbook_Open' event requires macro execution permissions. Check your Trust Center settings to ensure macros are enabled or that the document is in a Trusted Location.
How can I prevent users from bypassing the quiz by clicking the close (X) button on the UserForm?
You can disable the UserForm's close button by adding code to the 'UserForm_QueryClose' event. Add this line: If CloseMode = vbFormControlMenu Then Cancel = True. This forces the user to use your submit button.
Can I hide the background Excel sheets while the quiz is active?
Yes. In the 'Workbook_Open' event, before calling 'UserForm1.Show', you can set 'Application.Visible = False'. Just remember to set it back to 'True' when the correct answer is entered so the user can see the spreadsheet again.




