by Allen Wyatt
(last updated June 23, 2018)
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Copying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...Discover More
When creating a macro, one of the ways you can communicate with users is through the use of a message box. This tip ...Discover More
It can be frustrating when you get error messages doing something that you previously did with no errors. If you get an ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.