logo
search
VBA & Macro Problems

How to Create an Excel VBA Quiz That Closes the Workbook on a Wrong Answer

Huma Ashraf ChHuma Ashraf Ch Oct 1, 2026 869 views

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.

Create an Excel Quiz That Closes the Workbook After a Wrong Answer
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

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

2
Create the Custom UserForm

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.

3
Add the Validation Logic

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

4
Trigger the Form on Open

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

5
Save as a Macro-Enabled Workbook

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.

Use a VBA UserForm and Workbook Open Event
Macro Security: Users opening this file will need to click 'Enable Content' or have macros enabled in their Trust Center settings for the quiz to execute.
Advanced Automation

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. 1. Enable the Developer Tools: Open WPS Spreadsheet, go to the top ribbon, and click on the 'Developer' tab.
  2. 2. Launch the VBA Editor: Click the 'VBA Editor' icon to open the Visual Basic environment.
  3. 3. Build Your Quiz: Insert a UserForm and add your validation logic exactly as you would in standard Excel environments.
  4. 4. Save Your Work: Save your file in the .xlsm format to ensure full cross-compatibility and preserve your VBA project.
Fully compatible with Microsoft Excel .xlsm and .xlsb macro formats.Built-in Developer tools to easily write, debug, and execute VBA code.Supports standard Excel VBA syntax, including UserForm creation and workbook events.A lightweight, fast, and free alternative with a highly familiar user interface.
microsoft office alternative - wps office

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.