Determining Mouse Cursor Coordinates On a Graphic

Written by Allen Wyatt (last updated January 2, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


1

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Allowing Sentence Fragments

Grammar, particularly in English, has a perplexing array of rules and exceptions to those rules. Word does a fairly good ...

Discover More

Creating Scenario Summaries

If you've defined a variety of scenarios for your workbook, Excel can provide a handy way to compare the effects of those ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

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)

Selecting a Specific Cell in a Macro

Need to use a macro to select a specific cell in a different workbook? It's not as straightforward of a proposition as ...

Discover More

Finding the Path to the Desktop

Figuring out where Windows places certain items (such as the user's desktop) can be a bit frustrating. Fortunately, there ...

Discover More

Displaying the First Worksheet in a Macro

When creating macros, you often have to know how to display individual worksheets. VBA provides several ways you can ...

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}] (all 7 characters, in the sequence shown) 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 8 - 5?

2021-03-23 11:44:59

karmapala

Thank you very much for the tutorial, Sir.


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.