Sei sulla pagina 1di 5

01/11/2016 How to move and click the mouse in VBA?

 | Excel Help HQ

Home (http://excelhelphq.com/) /  Automation (http://excelhelphq.com/category/automation/) /  How to move and click the mouse in VBA?

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.

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/how­to­move­and­click­the­mouse­in­vba/ 1/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ

This entry was posted in   Automation (http://excelhelphq.com/category/automation/), Excel (http://excelhelphq.com/category/excel/), Popular


(http://excelhelphq.com/category/popular/), VBA Macro (http://excelhelphq.com/category/excel/vba-macro/).
Bookmark the   permalink (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/).

 This article was helpful

  3 people found this article useful

 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/)

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

af says:

why the rightclick same as left click

  June 11, 2013 at 7:10 pm (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=224#respond)

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?

Thank you for the post! Very usefull.

Thank you in advance


Regards!

  January 9, 2015 at 2:13 am (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=241#respond)

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

  October 25, 2013 at 6:38 am (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=230#respond)

click-the-mouse-in-vba/#comment-230)

Chip (http://excelhelphq.com) says:

Thanks for letting us know. It’s been 듚�xed.

  November 1, 2013 at 8:41 am (http://excelhelphq.com/how-to-move-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=234#respond)

and-click-the-mouse-in-vba/#comment-234)

Milind says:

Waw. Its fantastic. can you please add few more points in this?

* How to click and drag.

http://excelhelphq.com/how­to­move­and­click­the­mouse­in­vba/ 2/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ

  February 4, 2014 at 8:21 pm (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=231#respond)

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.

* How to press enter button.


* How to press Up Down Right Left arrow.

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

  February 4, 2014 at 8:27 pm (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=232#respond)

click-the-mouse-in-vba/#comment-232)

Aquarion says:

try this, its easy to use

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

  July 12, 2014 at 7:15 am (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=235#respond)

click-the-mouse-in-vba/#comment-235)

milu says:

How to type keyboard buttons?

  February 8, 2014 at 6:59 pm (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=233#respond)

click-the-mouse-in-vba/#comment-233)

admin (http://excelhelphq.com) says:

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)

  July 30, 2014 at 9:09 pm (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=236#respond)

click-the-mouse-in-vba/#comment-236)

Daniel says:

My excel says the “Public Declare” line contains an error, what should I do?

  September 26, 2014 at 12:09 pm (http://excelhelphq.com/how-to-move-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=237#respond)

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:

public declare PTRSAFE function setCursorPos…..

http://excelhelphq.com/how­to­move­and­click­the­mouse­in­vba/ 3/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ

  September 26, 2014 at 12:22 pm (http://excelhelphq.com/how-to-move-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=238#respond)

and-click-the-mouse-in-vba/#comment-238)

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…

  October 16, 2014 at 1:48 pm (http://excelhelphq.com/how-to-move-and-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=239#respond)

click-the-mouse-in-vba/#comment-239)

Gege says:

What about declarations in 64 bits version ?

  December 8, 2014 at 7:56 am (http://excelhelphq.com/how-to-move-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=240#respond)

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

  February 9, 2015 at 10:22 pm (http://excelhelphq.com/how-to-move-   Reply (http://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/?replytocom=242#respond)

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

Não sou um robô


reCAPTCHA
Privacidade - Termos

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/how­to­move­and­click­the­mouse­in­vba/ 4/5
01/11/2016 How to move and click the mouse in VBA? | Excel Help HQ

Post Comment

About Excel Help HQ

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

Access (http://excelhelphq.com/category/access/) (1)


Adobe (http://excelhelphq.com/category/adobe/) (1)
Autohotkey (http://excelhelphq.com/category/autohotkey/) (1)
Automation (http://excelhelphq.com/category/automation/) (3)
Downloads (http://excelhelphq.com/category/downloads/) (8)
Excel (http://excelhelphq.com/category/excel/) (45)
Charts (http://excelhelphq.com/category/excel/charts/) (4)
Data Table (http://excelhelphq.com/category/excel/data-table/) (3)
Excel 2003 (http://excelhelphq.com/category/excel/excel-2003/) (29)
Excel 2007 (http://excelhelphq.com/category/excel/excel-2007/) (30)
Excel 2010 (http://excelhelphq.com/category/excel/excel-2010/) (32)
Lookup (http://excelhelphq.com/category/excel/lookup/) (2)
VBA Macro (http://excelhelphq.com/category/excel/vba-macro/) (11)
Interviewing – Getting the Job (http://excelhelphq.com/category/interviewing-getting-the-job/) (3)
Other Tips (http://excelhelphq.com/category/other-tips/) (1)
Popular (http://excelhelphq.com/category/popular/) (4)
Powerpoint (http://excelhelphq.com/category/powerpoint/) (4)
Powerpoint 2007 (http://excelhelphq.com/category/powerpoint/powerpoint-2007/) (1)
Powerpoint 2010 (http://excelhelphq.com/category/powerpoint/powerpoint-2010/) (1)
VBA Macro (http://excelhelphq.com/category/powerpoint/vba-macro-powerpoint/) (2)
Windows (http://excelhelphq.com/category/windows/) (2)
Word (http://excelhelphq.com/category/word/) (2)
Work Smart (http://excelhelphq.com/category/work-smart/) (5)

http://excelhelphq.com/how­to­move­and­click­the­mouse­in­vba/ 5/5

Potrebbero piacerti anche