Move a Picture in an Excel UserForm with the Mouse

VBA & macros · Updated November 3, 2026

Move a Picture in an Excel UserForm with the Mouse

To move a picture around an Excel UserForm with the mouse, handle the Image control’s MouseDown and MouseMove events, then update its Left and Top properties while the left button is held. The example below also handles movement over the blank part of the form, so the picture can continue moving when the pointer leaves its edge.

What this technique does

An Excel UserForm is a custom dialog window built with VBA controls. A picture placed on it is usually an Image control, which displays a file or a picture loaded at runtime. To make that control draggable, VBA records where the pointer grabbed it, then changes the control’s position as the pointer moves.

This changes the position of the Image control on the form; it does not edit the picture file, move a worksheet image, or drag the picture into another window. The form must be open for the mouse event code to run. The approach is useful for movable previews, simple arrange-the-items interfaces, and forms where a user can reposition a graphic before continuing.

The sample assumes a control named Image1 on a UserForm. If your control has another name, replace Image1 in the code with its actual name. The code belongs in that UserForm’s code module, not in a standard module or a worksheet module.

Set up the UserForm and picture

  1. In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. On a Mac, the shortcut can depend on the keyboard and function-key settings; try Option+F11, or use Excel’s Tools menu to open the Visual Basic Editor.
  2. In the editor, insert a UserForm if you do not already have one. Open the Toolbox, select the Image control, and draw it on the form.
  3. With the Image control selected, use the Properties window to set its (Name) property to Image1. Set its Picture property to the image you want to display, or load a picture using VBA.
  4. Double-click the UserForm background to open its code module. Add the event procedures below there, changing the control name if needed.

To load a picture with code instead of the Properties window, you can assign a file path using the Image control’s Picture property and LoadPicture. Use a path that exists on the computer running the workbook; a path on your own machine may not exist for other users. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if it contains VBA.

Drag code for the Image control

Place this code in the UserForm module. It remembers the pointer’s grab point inside the image, moves the control while the left mouse button is down, and clears the dragging flag when the button is released.

Option Explicit

Private mDragging As Boolean
Private mGrabX As Single
Private mGrabY As Single
Private mFormGrabX As Single
Private mFormGrabY As Single

Private Sub Image1_MouseDown(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = 1 Then
        mDragging = True
        ' Keep the point inside the image where the user grabbed it.
        mGrabX = X
        mGrabY = Y
        ' The form-level events use coordinates relative to the form.
        mFormGrabX = Image1.Left + X
        mFormGrabY = Image1.Top + Y
    End If
End Sub

Private Sub Image1_MouseMove(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If mDragging And (Button And 1) <> 0 Then
        ' Image event coordinates are relative to the Image control.
        Image1.Left = Image1.Left + X - mGrabX
        Image1.Top = Image1.Top + Y - mGrabY
    End If
End Sub

Private Sub Image1_MouseUp(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    mDragging = False
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If mDragging And (Button And 1) <> 0 Then
        ' Continue the drag over the blank background of the form.
        Image1.Left = X - mFormGrabX
        Image1.Top = Y - mFormGrabY
    End If
End Sub

Private Sub UserForm_MouseUp(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    mDragging = False
End Sub

The <> operator means “not equal to” in VBA. In the HTML code block it is written with escaped angle brackets so it displays correctly. The check tests whether the left-button bit is still set in the Button argument. The form-level handlers are helpful when the pointer leaves the image but remains over the form. The image-level and form-level mouse coordinates have different origins, which is why the code stores both the image grab point and the grab point relative to the form.

How the position calculation works

MSForms positions controls using points, not worksheet row and column coordinates. The Image control’s Left and Top values describe its position within its container: Left is the horizontal distance from the container’s left edge, and Top is the vertical distance from its top edge. The mouse event arguments X and Y report the pointer position for that event.

On MouseDown, the code saves the pointer’s location inside the picture. If it did not remember that offset, it could snap the picture’s top-left corner to the pointer as soon as dragging began. During a drag, the code adjusts Left and Top so the same point on the picture stays under the pointer. This is why the image follows naturally whether the user grabs its middle or a corner.

The MouseMove event can fire repeatedly as the pointer moves. The button check prevents ordinary movement over the picture from dragging it when the mouse button is not held. The MouseUp handlers reset the flag so that a later pointer movement does not continue a previous drag.

Test the drag and save safely

Run the form from the Visual Basic Editor or from a macro that calls its Show method. Grab different areas of the picture and move it slowly, first over the image and then across the blank form background. Confirm that it follows the pointer without jumping and that releasing the left button stops it.

  • If the form is opened by a button or another macro, make sure that code displays the correct UserForm instance.
  • If the picture does not appear, check the Image control’s Picture property and whether the image path is valid.
  • Before running or changing a macro, save a copy of the workbook. VBA macros cannot be undone with Ctrl+Z on Windows; Excel’s undo history does not reverse VBA actions.
  • When you share the workbook, tell users that macros must be enabled for the drag behavior to work. They should follow their organization’s macro-security rules rather than lowering security settings globally.

Keep the picture inside the form

The sample allows the image to move beyond the form’s visible edges. That can be useful if users are meant to position it freely, but it may also make the picture hard to retrieve. To constrain it, clamp its new position between zero and the available space after each drag calculation.

For example, after calculating a proposed horizontal position, use the greater of zero and the smaller of that position and the form’s inside width minus the image width. Apply the same idea vertically with the inside height and image height. In VBA, the built-in Min and Max functions are not available as general VBA functions in the same way they are in worksheet formulas, so use simple If statements or a small helper function for the bounds. Also consider whether the form has a border or other controls that should define a smaller usable area.

If the image is larger than the available area, there is no position that can keep all of it inside the form. Decide whether to resize the picture, allow partial clipping, or set a minimum form size. For a simple form, changing the Image control’s size in the Properties window may be enough. Remember that changing its displayed dimensions does not necessarily change the dimensions or quality of the source image.

Common problems and practical fixes

  • The picture moves only while the pointer is over it. Check that both UserForm_MouseMove and UserForm_MouseUp are in the same form module as the Image control’s handlers. The form-level movement handler helps continue a drag over the form’s blank background. It cannot guarantee continued tracking if the pointer leaves the form window altogether.
  • The image jumps when dragging starts. Make sure the code stores the mouse-down coordinates and uses them as the grab offset. Setting Image1.Left = X directly treats a pointer coordinate as a form position and usually causes a jump because the coordinate origins differ.
  • Dragging does not start. Confirm that the code is in the correct UserForm module and that the control name in the procedure matches the control’s (Name) property. In the Visual Basic Editor, selecting the Image control and choosing its event from the procedure list can create correctly named event stubs.
  • The picture moves but will not stop. Keep the MouseUp procedures. A release over the blank form is handled by the form’s handler, while a release over the image is handled by the Image control’s handler.
  • The picture looks stretched or cropped. That concerns the Image control’s display settings, not the drag calculation. Review its PictureSizeMode property and control dimensions. A picture that looks distorted may need a different sizing mode or a source image with a matching shape.

Mouse events are not the same as operating-system-level mouse capture. If the user presses the button on the image and releases it after moving outside the entire UserForm, the form may not receive the release event. The example covers movement and release over the form, which suits many small interfaces. Applications that require reliable tracking beyond the form window need a more advanced Windows API approach; that adds platform-specific declarations and extra compatibility and maintenance concerns.

Windows, Mac, and worksheet pictures

This example uses standard MSForms mouse events and does not call Windows APIs. That avoids API declarations that can differ between 32-bit and 64-bit Windows and do not directly transfer to Mac. Excel for Mac supports VBA and UserForms with differences from Windows, but behavior and available controls can vary by Excel release. If the workbook must run on both platforms, try the form and its mouse events in the specific Mac version you support rather than assuming every Windows form behaves identically.

Excel for the web does not run VBA macros, so this UserForm interaction will not work in a browser. A user who needs the drag interface must open the macro-enabled workbook in a desktop version of Excel with macros permitted. Also, dragging an Image control on a UserForm is separate from moving a picture placed directly on a worksheet: worksheet pictures are drawing objects and use Excel’s normal selection and drag behavior, not UserForm event procedures.

Alternatives and when to use them

If the picture only needs to be repositioned while designing the form, there is no need for runtime VBA: select the Image control in the UserForm designer and drag it to the desired spot. If users must move it while the form is open, the event approach above is the direct VBA solution.

For a more controlled interface, consider adding buttons such as “Move left” and “Move right,” or fields where users enter coordinates. These options can be easier to use with a keyboard, touchpad, or assistive technology, and they avoid some limitations of mouse tracking. If the goal is to let people arrange worksheet graphics rather than controls in a dialog, place pictures on the worksheet and use Excel’s drawing-object features instead. Choose the simplest interaction that matches where the picture needs to end up.

Frequently Asked Questions

Can I drag a picture on a UserForm without VBA?
No, not as a runtime interaction using this standard Image control setup. You can reposition the control in the UserForm designer without code, but making it follow a user’s mouse while the form is open requires event code or another custom interaction.

Does this move the original image file?
No. The code moves the Image control within the UserForm. It does not change, rename, or relocate the image file on disk, and it does not change a picture stored on a worksheet.

Why does the picture snap to the mouse pointer?
That usually happens when the code places the control’s top-left corner at the current pointer position. Store the mouse-down position inside the image and preserve that offset during movement, as the sample does.

Will the drag work in Excel for the web?
No. Excel for the web does not execute VBA macros or display this VBA UserForm interaction. Open the workbook in a compatible desktop Excel application to use the macro.

Can I keep the picture from moving outside the form?
Yes. After calculating its new position, limit Left to the range from zero to the form’s usable width minus the image width, and limit Top similarly. Use the form’s actual usable area if other controls or borders reduce the space available.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.