Thread: Drag picture
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default Drag picture

can anyone tell me how to make the picture box in the excel
form be dragable by using VBA programming?


You said "picture box", but I am going to assume you meant ImageBox. Place
an ImageBox (leave the default name of Image1) on the form. Copy/paste the
code below my signature into the UserForm's code window, run the project and
press Shift+<LeftMouseButton to drag the ImageBox around the form.

Rick

Private OffsetX As Long, OffsetY As Long
Private Moving As Boolean

Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 And Shift = 1 Then
OffsetX = X
OffsetY = Y
Moving = True
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 Moving Then
With Image1
.Move .Left + X - OffsetX, .Top + Y - OffsetY
End With
End If
End Sub

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