logo
search
VBA & Macro Problems

How to Enlarge a Selected VBA UserForm Image in a Second UserForm

Natalie TaylorNatalie Taylor Oct 16, 2026 868 views

Question details

The user needs to dynamically open and enlarge an image from a main VBA UserForm into a second UserForm when clicked, requiring a scalable solution to handle approximately 100 images via a loop.

How to Enlarge a Selected VBA UserForm Image in a Second UserForm
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Managing a large volume of images inside a main UserForm and needing to individually preview or zoom in on them without manually writing separate click event codes for each individual image.
Observed behavior
The user wants to load a clicked image into a separate zoomed UserForm by dynamically assigning the Picture property using a WithEvents class module and a loop.
Before you start

Ensure that macros are enabled in your spreadsheet settings and that you have basic familiarity with the VBA Editor, including how to create UserForms and insert Class Modules.

Solution 1Recommended

Use a WithEvents Class Module and a Collection

This is the most efficient method for handling events for multiple controls. Instead of writing 100 individual click events, you create one event-handling class and iterate through the controls using a loop.

By creating a custom class module with a WithEvents variable, you can capture events (such as MouseUp or Click) for any control assigned to it. Storing these object instances in a module-level Collection ensures they remain active in memory as long as the main UserForm is open.

1
Create the Zoom UserForm

Insert a new UserForm named `frmZoom` in the VBA Editor. Add an Image control named `imgZoom`. To allow closing via the Escape key, add a CommandButton, set its `Cancel` property to True, and add `Unload Me` to its Click event.

2
Create the Image Handler Class Module

Insert a Class Module and name it `MyImageHandler`. Declare a variable at the top: `Public WithEvents img As MSForms.Image`. Then, write the `img_MouseUp` event procedure to assign the picture: `frmZoom.imgZoom.Picture = img.Picture` and call `frmZoom.Show`.

3
Initialize the Main UserForm Collection

In your main UserForm (e.g., `UserForm1`), declare a module-level Collection at the very top of the code window (before any macros): `Dim ImageHandlers As New Collection`.

4
Loop and Bind the Controls

Inside the `UserForm_Initialize` event of the main UserForm, write a `For Each ctrl In Me.Controls` loop. If `TypeOf ctrl Is MSForms.Image`, instantiate a new `MyImageHandler`, assign `Set handler.img = ctrl`, and add the handler to the `ImageHandlers` collection.

Use a WithEvents Class Module and a Collection
Scalable Code Approach: This loop-and-collection method efficiently handles an unlimited number of images without bloating your code with hundreds of repetitive event blocks.
Advanced Macros in WPS Spreadsheet

Execute VBA and Macros Seamlessly with WPS Office

WPS Spreadsheet provides excellent support for VBA and Macros, allowing you to create complex UserForms, Class Modules, and automated tasks just like you would in Microsoft Excel.

  1. 1. Open your macro-enabled file: Launch WPS Spreadsheet and open your existing .xlsm or .xlsb document.
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon. If it is hidden, enable it in the customized ribbon settings.
  3. 3. Open the VBA Editor: Click the 'Visual Basic' icon or press Alt + F11 to launch the integrated VBA Editor.
  4. 4. Manage UserForms and Modules: Use the Project Explorer on the left to insert UserForms, Standard Modules, and Class Modules.
  5. 5. Run your macros: Test your image enlargement UserForm directly within WPS Spreadsheet with full compatibility.
Native support for VBA (Visual Basic for Applications) scripts.Full compatibility with Microsoft Excel .xlsm and .xlsb macro-enabled formats.Comprehensive UserForm and ActiveX control environment.Lightweight installation with powerful automation features.
microsoft office alternative - wps office

Frequently Asked Questions

Why do the image click events stop working after the main UserForm loads?

If you do not store your class instances in a module-level Collection, the local variables go out of scope and are destroyed as soon as the Initialize event finishes executing. Keeping them in a Collection prevents them from being deleted, ensuring the WithEvents handlers continue to work.

How can I close the zoomed UserForm using the Escape key?

Add a CommandButton to your `frmZoom` UserForm and set its `Cancel` property to True in the Properties window. Write `Unload Me` in its Click event. When the user presses the Escape key, this button's code will automatically trigger.

Can I use the Click event instead of MouseUp for UserForm images?

Yes, you can use the `Click` event, but `MouseUp` is often preferred for Image controls because it can capture exact mouse coordinates and specific mouse button interactions (left or right click) if you need more advanced functionality later.

Does WPS Spreadsheet fully support VBA Class Modules?

Yes, WPS Office Pro and specific business editions natively include a fully functional VBA environment that handles standard modules, complex UserForms, and Class Modules seamlessly.