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
why the rightclick same as left click
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!
Excellent example. Thanks for the post. Just minor error. The Single Right Click example executes left click. Replace the LEFT constants to RIGHT
Thanks for letting us know. It’s been fixed.
Waw. Its fantastic. can you please add few more points in this?
* How to click and drag.
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.
try this, its easy to use
http://msdn.microsoft.com/en-us/library/office/ff821075(v=office.15).aspx
How to type keyboard buttons?
Here you go. This new post shows you how to use VBA to send keystrokes. https://excelhelphq.com/how-to-type-on-keyboard-in-vba
My excel says the “Public Declare” line contains an error, what should I do?
Ok!! Found my own answer! In VBA7 you need to declare it using the “PtrSafe” keyword:
public declare PTRSAFE function setCursorPos…..
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…
What about declarations in 64 bits version ?
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 😐