Get mouse coordinates with simple macro?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Hi All,

Following this recent question on creating mouse-interactive embedded charts (http://www.mrexcel.com/forum/excel-...ouse-interactive-chartobject.html#post3451252), I have struggled to set up a class module with the mousedown event, because I have many sheets with embedded charts, some of which must have the same name because the sheets are copied from a common template.

Therefore, I was wondering if it is possible to get the mouse coordinates/movements without using a class module and mousedown event, by assigning a simple macro to my embedded chart so that the macro is activated to record the mouse coordinates/movements when I click on the chart?

This would certainly make coding much simpler, if someone could please suggest what command I need in my macro to track the mouse...

Thanks in advance for any help.

Best regards,
Kelvin
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this:
Code:
Dim MyPointAPI As POINTAPI
Private Type POINTAPI
  x As Long
  y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Then:
Code:
Public Sub MouseCursorGetOurXY()
    Dim l As Long

    l = GetCursorPos(MyPointAPI) 'get our cursor position first
    msgbox CStr(MyPointAPI.x) & ", " & CStr(MyPointAPI.y) 'displays cursor X Y coordinates
End Sub
 
Last edited:
Upvote 0
Thanks, Chuck. Where do I put this code? How does it work? Will it tell me where the mouse is clicked and dragged to (start and end coordinates), and whether Shift and/or Control was pressed?
 
Upvote 0
Put both code sections above in a module. Then run the 2nd code sub. It will give you the X, Y coordinates of wherever your mouse is at that time.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top