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

Nudging a Graphic

Want to get a graphic to just the right position on a worksheet? Sometimes the easiest way is to use the arrow keys on the ...

Discover More

Hanging Indents in Wrapped Text

If you use hanging indents for some of your paragraphs, you may wonder why they don't look right when they wrap on the right ...

Discover More

Limiting Entry of Names

When inputting information into a worksheet, you may need a way to limit what can be entered. This scenario is a prime ...

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)

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference in ...

Discover More

Removing a Macro from a Shortcut Key

When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...

Discover More

Creating and Naming a Worksheet Using a Macro

You can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...

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. Maximum image size is 8Mpixels. 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 eight more than 8?

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.