Yes, you can drag an image inside an Image Box using Excel VBA by implementing the MouseDown, MouseMove, and MouseUp events to track and change the Picture property dynamically, but this requires the image to be a standard bitmap, GIF, or JPEG linked to the sheet rather than embedded as an object. This technique allows users to visually move a picture around a specific area on a UserForm or worksheet, useful for creating interactive reports or simple image viewers without relying on third-party libraries. The core mechanism involves calculating the difference between the mouse cursor's current position and its starting position whenever the button is held down, then applying that offset to the Top and Left properties of the image control.
What You'll Need
- A Microsoft Excel application installed on your computer (2010, 2013, 2016, 2019, or Excel for Microsoft 365).
- At least one Image control on a UserForm or a Worksheet (for the worksheet method, the image must be an object linked to a file).
- Visual Basic for Applications (VBA) editor access.
- A source image file (e.g., a JPG or PNG) saved locally to ensure the code can reference the path correctly.
Step-by-Step
The following guide outlines two methods: dragging an image on a UserForm and dragging a linked image on a Worksheet. The UserForm method is generally more stable for this specific task.
Method 1: Dragging an Image on a UserForm
- Open the VBA Editor: Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor. This is where you will write the code.
- Insert a UserForm: In the VBA editor, go to the menu bar and select Insert > UserForm. A blank form will appear on the screen.
- Add the Image Control: Locate the Toolbox window (usually on the left side). If it is not visible, you can enable it by going to View > Toolbox. Drag an Image control from the toolbox onto the UserForm. Resize the image box to your desired dimensions.
- Load the Image: With the Image control selected on the form, look at the Properties window (usually on the right side). Find the Picture property and click the ellipsis (...) button. Navigate to your image file and load it into the control. Alternatively, you can load the image programmatically using code, but using the properties window is easier for testing.
- Add the Code Module: Right-click anywhere in the Project Explorer (usually the top-left area of the VBA editor). Select Insert > Module. This module will hold the code to show the UserForm when the button is clicked.
- Write the Button Code: In the Module, paste the following code to create a button that opens the form and allows dragging:
Sub ShowDragForm() UserForm1.Show End Sub - Open the UserForm Code Window: Double-click on the UserForm itself (not the image). This will open the code window specific to that form.
- Implement the Drag Logic: In the UserForm code window, you need to write the code for the Mouse events. Paste the following code into the UserForm code module. This code sets up variables to track the starting position of the mouse and the starting position of the image.
Private dragging As Boolean Private startX As Long Private startY As Long Private imgX As Long Private imgY As Long Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ' Check if the left mouse button is pressed If Button = 1 Then dragging = True ' Store the initial position of the mouse cursor relative to the image control startX = X startY = Y ' Store the current Top and Left values of the image imgX = Image1.Left imgY = Image1.Top End If End Sub Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) ' Only proceed if dragging is active and the left mouse button is held If dragging And Button = 1 Then ' Calculate the difference between the current mouse position and the starting position Dim moveX As Long Dim moveY As Long moveX = X - startX moveY = Y - startY ' Apply the calculated difference to the image's position ' Add the current image position to ensure smooth dragging Image1.Left = imgX + moveX Image1.Top = imgY + moveY End If End Sub Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) ' Stop the dragging process when the mouse button is released dragging = False End Sub - Test the UserForm: Close the code window and return to the Excel window. Press Alt + F8, select ShowDragForm, and click Run. Your UserForm will open. Click and hold the left mouse button on the image inside the box to drag it around. Release the mouse button to stop.
Method 2: Dragging a Linked Image on a Worksheet
Dragging an image on a worksheet is more complex because the standard Image control is not available on sheets. Instead, you must use a linked object.
- Insert the Image as an Object: Go to your Excel worksheet. Go to the Insert tab and select Object from the Text group. In the dialog box, choose From File and select your image. Ensure the Link to file checkbox is checked. This creates an object that is linked to your image file.
- Resize the Object: Click on the object and drag the corner handles to resize it to your preferred size. Note the specific location where you place it, as you will need to reference these coordinates in your code.
- Open the VBA Editor: Press Alt + F11 to open the editor.
- Add the Worksheet Code: In the Project Explorer, locate your workbook. Double-click on the specific worksheet name (e.g., Sheet1) where you placed the image. This opens the code window for that sheet.
- Implement the Drag Logic: Paste the following code into the worksheet code module. This code uses the
Shapeobject to represent the image and tracks the mouse position relative to the shape.Private dragging As Boolean Private startX As Long Private startY As Long Private shapeX As Long Private shapeY As Long Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' Optional: Deselect any shape when clicking elsewhere ' If Not dragging Then ' On Error Resume Next ' ActiveSheet.Shapes("Picture 1").Select ' On Error GoTo 0 ' End If End Sub Private Sub Worksheet_MouseDown(ByVal Target As Range, ByVal Shift As Integer, ByVal Button As Integer) ' Check if the left mouse button is pressed and if we clicked on a shape If Button = 1 Then Dim shp As Shape Set shp = ActiveSheet.Shapes("Picture 1") ' Replace "Picture 1" with your exact name If Not shp Is Nothing Then dragging = True ' Store the initial position of the mouse relative to the shape startX = Application.WindowState = xlNormal Then ActiveWindow.PointsToScreenPixelsX(Target.Left) Else 0 startY = Application.WindowState = xlNormal Then ActiveWindow.PointsToScreenPixelsY(Target.Top) Else 0 ' Store the current Top and Left values of the shape shapeX = shp.Left shapeY = shp.Top End If End If End Sub Private Sub Worksheet_MouseMove(ByVal Target As Range, ByVal Shift As Integer, ByVal Button As Integer) If dragging And Button = 1 Then Dim shp As Shape Set shp = ActiveSheet.Shapes("Picture 1") If Not shp Is Nothing Then ' Calculate the difference between the current mouse position and the starting position Dim moveX As Long Dim moveY As Long moveX = X - startX moveY = Y - startY ' Apply the calculated difference to the shape's position shp.Left = shapeX + moveX shp.Top = shapeY + moveY End If End If End Sub Private Sub Worksheet_MouseUp(ByVal Target As Range, ByVal Shift As Integer, ByVal Button As Integer) dragging = False End Sub - Test the Worksheet: Return to your Excel sheet. Click and hold the image object to drag it. The code attempts to track the mouse position; however, this method is often less precise than the UserForm method due to screen coordinate system differences.