VBA & Macro Problems

How to Keep an Excel UserForm Fixed and Prevent Closing

Steve KSteve K Oct 12, 2026 871 views

Question details

The user needs to lock an Excel VBA UserForm's position to the center of the screen and disable the ability to move or close it until correct credentials are provided.

How to Keep an Excel UserForm Fixed and Prevent Closing
Product
Microsoft Excel
Device & OS
Windows
Scenario
Creating a secure login or credential prompt in Excel where the user must enter valid data before proceeding, without bypassing the prompt by moving or closing the window.
Observed behavior
By default, a UserForm can be dragged around the screen by its title bar and closed via the 'X' button, allowing users to bypass the intended credential check.
Before you start

Ensure you have the Developer tab enabled in your ribbon and make a backup of your workbook before modifying VBA code that contains Windows API calls.

Solution 1Recommended

Use Windows API and VBA to Lock the UserForm

Apply Windows API declarations within a standard module to modify the window styles of the UserForm, hiding or disabling the control menu and close button upon initialization.

To fundamentally change how a UserForm behaves in the Windows environment, standard VBA properties are often insufficient. By calling the Windows API directly, you can remove the title bar's interactive elements, ensuring the form cannot be moved or closed manually.

1
Insert a New Module

Open the Visual Basic Editor (Alt + F11). Select 'Insert' > 'Module' from the top menu, or use the keyboard shortcut Alt + I, then M, to create a new standard code module.

2
Add Windows API Declarations

In the new module, paste your required Windows API declarations (such as FindWindow and GetWindowLong) and create a procedure (e.g., FormatUserForm) that alters the window style to disable dragging and the close button.

3
Center the UserForm

Select your UserForm in the Project Explorer. In the Properties window (F4), find the 'StartUpPosition' property and change it to '2 - CenterScreen' to ensure it always opens perfectly centered.

4
Call the Formatting Procedure

Double-click your UserForm to open its code module. From the dropdowns at the top, create a 'UserForm_Initialize' event. Inside this event, add the line 'FormatUserForm Me.Caption' so the API restrictions are applied immediately when the form loads.

5
Validate Credentials

Add command buttons and text boxes for the user's credentials. In the Submit button's click event, verify the plant credentials. Only execute the 'Unload Me' command once the correct information is verified.

Use Windows API and VBA to Lock the UserForm
Safety Precaution: Always ensure you have an alternative way to close the form (like a verified 'Submit' button) before testing API code that disables the 'X' button, otherwise you may have to force quit Excel.
Advanced VBA Support

Create and Manage Macros with WPS Spreadsheets

WPS Office provides robust, built-in support for VBA and Macros. You can easily insert UserForms, write VBA scripts, and run advanced Windows API calls directly within WPS Spreadsheets, just as you would in Microsoft Excel.

  1. 1. Open WPS Spreadsheets: Launch WPS Office and open your macro-enabled workbook.
  2. 2. Access the Developer Tab: Click on the 'Developer' tab in the top ribbon menu. If it's not visible, enable it from the WPS settings menu under 'Options'.
  3. 3. Open the VBA Editor: Click the 'Visual Basic' icon or press Alt + F11 to launch the integrated VBA Editor in WPS Office.
  4. 4. Design Your UserForm: Select 'Insert' > 'UserForm' to build your interface, and add your API code into a standard module to lock the form's position.
Free and lightweight Office suiteFully compatible with Microsoft Excel (.xlsm) macro formatsBuilt-in Visual Basic Editor for UserForms and standard modulesSeamless migration of existing Excel VBA projects without rewriting code
microsoft office alternative - wps office

Frequently Asked Questions

How do I create a new module in the Visual Basic Editor?

In the Visual Basic Editor, you can create a new code module by clicking 'Insert' on the top menu bar and selecting 'Module'. Alternatively, use the keyboard shortcut Alt + I followed by M.

Why can't I just use Cancel = True in the QueryClose event?

Using `Cancel = True` inside the `UserForm_QueryClose` event does prevent the form from closing when the 'X' button is clicked. However, it does not prevent the user from dragging and moving the UserForm around the screen. To completely lock its position, modifying the window style via Windows API is required.

How can I center the UserForm on the screen without using VBA code?

You can center a UserForm statically by selecting it in the VBA editor, pressing F4 to open the Properties window, and changing the `StartUpPosition` property to `2 - CenterScreen`.