Determining Mouse Cursor Coordinates On a Graphic

by Allen Wyatt
(last updated August 10, 2013)

5

Gerald notes that when moving the mouse over a picture on a worksheet the mouse pointer is a cross. He wants to know how he can determine, using VBA, the coordinates of the cross when the mouse is clicked.

Excel doesn't allow you (even with VBA) to get the coordinates of the mouse pointer on a graphic inserted as a regular picture in the worksheet. If you insert the picture using an Image object in the Control toolbox, you have quite a bit more latitude. Indeed, you can use the MouseDown event handler to determine the coordinates, as shown here:

Private Sub Image1_MouseDown(ByVal Button As Integer, _
  ByVal Shift As Integer, ByVal X As Single, _
  ByVal Y As Single)
    MsgBox X & ", " & Y
End Sub

This code assumes that the image is named Image1. Similar code could be used to display the cursor coordinates in real time on the status bar:

Private Sub Image1_MouseMove(ByVal Button As Integer, _
  ByVal Shift As Integer, ByVal X As Single, _
  ByVal Y As Single)
    Application.StatusBar = Round(X, 0) & "," & Round(Y, 0)
End Sub

Either (or both) of these event handlers are obviously associated with Image1, so they need to be added to the code window for that object.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3421) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Inserting the Total Number of Pages in Your Document

Word keeps track of many statistics for each of your documents. One statistic is the total number of pages in the printed ...

Discover More

Setting Default Print Margins

Don't like the print margins that Excel uses by default? You can change the default by changing the workbook on which Excel ...

Discover More

Easily Adding a Graphic to a Document

Need to spice up your document with a graphic? Here's the quickest way we've found to get those graphics where you want them.

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Delimited Text-to-Columns in a Macro

The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...

Discover More

Assigning Macros to Graphics

The graphics you place in a worksheet can do more than just look pretty. You can also assign macros to a graphic, which are ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time figuring ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 0?

2016-11-28 13:14:33

Peter

Thanks! This was informative.

Is it possible to use these coordinates to draw on the image itself? Like a density diagram.
I would like to draw dots on the selected location and keep the x ,y coordinate as part of a database.


Thanks again!


2016-04-13 08:11:37

Nader

Helped a lot! Thank you.


2014-12-22 15:36:19

Phil

This was very helpful and prompted me to try something.

I have a sequence of JPGs and use command buttons to advance/reverse through them one at a time. Those work, but when I click on the image to call the Image1_MouseDown SUB and display the X and Y coordinates in separate textboxes on the same form, the advance/reverse no longer functions.

I must be missing something obvious. Any advice would be greatly appreciated.


2014-12-03 17:12:36

Brian

This works very well as long as the image is not too large. I haven't been able to figure out the maximum size, but it is around 12 mega pixels. Unfortunately for me, I want 11 x 17 at 300 dpi image. It doesn't work.

I have tried to find a work around, but the only work around I have found is decreasing the resolution which is not ideal.


2014-05-27 13:53:53

Davi

Hello,

I'm getting the coordinates of the mouse with my VBA macro.

But all the coordinates are integers. Is there any way I can get more accuracy?

I'm needing at least 3 three decimal places...

Thank you!!


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.