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

Triple-Spacing Your Document

Print your document with lots of space between each line—triple space it! Here's some quick and easy steps for getting ...

Discover More

Controlling the Printing of Highlighting

Using Word's built-in highlighter tool can be a great way to add markup to a document and attract a reader's eyes to specific ...

Discover More

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro in ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

MORE EXCELTIPS (MENU)

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

Creating Add-Ins

Want to create your own add-in? Excel makes it easy to do. Here are all the steps you need.

Discover More

Running Macros on Hidden Worksheets

Excel allows you to hide worksheets so that they aren't visible to those using your workbook. Hiding worksheets has a ...

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 for this tip:

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 9 - 7?

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.

Links and Sharing