Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
| Excel Help HQ
Home (http://excelhelphq.com/) / Automation (http://excelhelphq.com/category/automation/) / How to move and click the mouse in VBA?
Gartner Magic
Quadrant
Just Released! Analytics &
Business Intelligence
Leaders. Free Report.
tableau.com
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 듚�rst 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 A
s 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
Related Posts
How to read or access the clipboard with Excel VBA? (http://excelhelphq.com/how- Commonly used Excel VBA snippets (http://excelhelphq.com/commonly-used-
to-read-or-access-the-clipboard-with-excel-vba/) excel-vba-snippets/)
How to precisely move objects / shapes in Powerpoint in VBA? Waterfall chart template download with instructions (supports negative values)
(http://excelhelphq.com/precisely-move-objects-shapes-in-powerpoint-in-vba/) (http://excelhelphq.com/waterfall-chart-template-download-with-instructions-
supports-negative-values/)
How to type on the keyboard in VBA? (http://excelhelphq.com/how-to-type-on-
keyboard-in-vba/) How to 듚�x the vlookup function? Help! It doesn’t work and give me errors!
(http://excelhelphq.com/how-to-듚�x-the-vlookup-function-help-it-doesnt-work-and-
give-me-errors/)
http://excelhelphq.com/howtomoveandclickthemouseinvba/ 1/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ
Commonly used Excel VBA snippets (http://excelhelphq.com/commonly-used- How to read or access the clipboard with Excel VBA?
excel-vba-snippets/) (http://excelhelphq.com/how-to-read-or-access-the-clipboard-with-excel-vba/)
af says:
click-the-mouse-in-vba/#comment-224)
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 듚�les 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?
click-the-mouse-in-vba/#comment-241)
Viewer says:
Excellent example. Thanks for the post. Just minor error. The Single Right Click example executes left click. Replace the LEFT constants to RIGHT
click-the-mouse-in-vba/#comment-230)
and-click-the-mouse-in-vba/#comment-234)
Milind says:
Waw. Its fantastic. can you please add few more points in this?
http://excelhelphq.com/howtomoveandclickthemouseinvba/ 2/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ
click-the-mouse-in-vba/#comment-231)
Milind says:
As well as i want to know how to press keyboard buttons. Most importent for for my daly work is.
click-the-mouse-in-vba/#comment-232)
Aquarion says:
http://msdn.microsoft.com/en-us/library/of듚�ce/ff821075(v=of듚�ce.15) (http://msdn.microsoft.com/en-us/library/of듚�ce/ff821075(v=of듚�ce.15)).aspx
click-the-mouse-in-vba/#comment-235)
milu says:
click-the-mouse-in-vba/#comment-233)
Here you go. This new post shows you how to use VBA to send keystrokes. http://excelhelphq.com/how-to-type-on-keyboard-in-vba (http://excelhelphq.com/how-to-
type-on-keyboard-in-vba)
click-the-mouse-in-vba/#comment-236)
Daniel says:
My excel says the “Public Declare” line contains an error, what should I do?
and-click-the-mouse-in-vba/#comment-237)
Daniel says:
Ok!! Found my own answer! In VBA7 you need to declare it using the “PtrSafe” keyword:
http://excelhelphq.com/howtomoveandclickthemouseinvba/ 3/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ
and-click-the-mouse-in-vba/#comment-238)
Rick says:
Hello,
Thanks…
click-the-mouse-in-vba/#comment-239)
Gege says:
and-click-the-mouse-in-vba/#comment-240)
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
and-click-the-mouse-in-vba/#comment-242)
Leave a Reply
Your email address will not be published. Required 듚�elds are marked *
Name *
Email *
Website
Comment
You may use these HTML (HyperText Markup Language) tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite="">
<cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
http://excelhelphq.com/howtomoveandclickthemouseinvba/ 4/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ
Post Comment
Excel Help HQ is created by a few frequent Excel users to share knowledge and help others on Excel problems. We focus on more obscure problems, memory aids and
specialized vba macro scripts for other heavy Excel users. Feel free to click on any category on the right to 듚�nd tips and tricks that will help you be better in Excel and
other frequently used tools in the work place!
Ads by Google
Excel VBA Code
Download Excel
Search Knowledgebase…
Categories
http://excelhelphq.com/howtomoveandclickthemouseinvba/ 5/5