How to move and click the mouse in VBA?

You could use Excel VBA to move the mouse and click on things (left and right click). Below is an example of moving the mouse to the top left of the screen and then clicking. Just copy the code and paste it into macro window in Excel.

The SingleClick() subroutine is a single click, while DoubleClick() subroutine does a double click. The code is quite self explanatory and needs minimal instructions.

Note that SetCursorPos moves the mouse based on the coordinates supplied. The first parameter is the # of pixels to the right from the top left corner of the monitor (x-axis) and the second parameter is the # of pixels below the top left corner of the monitor (y-axis). If the user is using duel monitors, it will be top left corner of the the left most monitor.

Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10

Private Sub SingleClick()
  SetCursorPos 100, 100 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub DoubleClick()
  'Double click as a quick series of two clicks
  SetCursorPos 100, 100 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub RightClick()
  'Right click
  SetCursorPos 200, 200 'x and y position
  mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub

14 thoughts on “How to move and click the mouse in VBA?

    1. Alex says:

      Hi, it’s possible to VBA react to a new window that appears or a reaction from PC?
      I need to download some files every day, I have to do credencials in web browser and more steps so I can’t just put the link to download.
      If VBA could react to do the next step if a window appears or if some image in the screen appears would be great. Any solution? suggestions?

      Thank you for the post! Very usefull.

      Thank you in advance 🙂
      Regards!

  1. Milind says:

    As well as i want to know how to press keyboard buttons. Most importent for for my daly work is.

    * How to press enter button.
    * How to press Up Down Right Left arrow.

    I think it is keyboard events. Please help to resolve this.

  2. Rick says:

    Hello,

    How do I get this to work in “Excel for Mac 2011” v14.4.4

    I get an error with “SetCursorPos” and “mouse_event”…

    Do I need to create a function that works with this code?

    Thanks…

  3. wola says:

    I copy the code into module, but it does not work. I have been trying to make the code work, but I have no luck so far. i wanna make my mouse move and click 😐

Cancel reply

Leave a Reply to af

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>