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

Calculating a Sum for a Range of Dates

If you use Excel to track information based on dates, you may wonder how to get a sum for only certain dates that you ...

Discover More

Hyperlinks in Shared Workbooks

Inserting a hyperlink into a workbook that is shared with others is not possible in Excel. Here's what you can do about it.

Discover More

Filling a Range of Cells with Values

When writing a macro, you may want to fill a range of cells with different values. The easiest way to do this is to use ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Transferring Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to move data from one ...

Discover More

Moving Macros from the Personal Workbook

Need to move a macro out of your Personal.xls workbook and into a regular workbook? You can do it using familiar editing ...

Discover More

Setting Row Height in a Macro

Macros can be used to change the formatting of your worksheet, if desired. One change you might want to make is to the height ...

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 6Mpixels. 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 nine minus 3?

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.