Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Determining Mouse Cursor Coordinates On a Graphic

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Peter    28 Nov 2016, 13:14
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!
Nader    13 Apr 2016, 08:11
Helped a lot! Thank you.
Phil    22 Dec 2014, 15:36
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.
Brian    03 Dec 2014, 17:12
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.
Davi    27 May 2014, 13:53
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!!
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.