Cursor Position

thewilf

New Member
Joined
Apr 13, 2011
Messages
10
Hello All,

Just a quick question for you, I have a series of sheets which a user moves through by clicking on buttons. I would then like for them to get to a sheet where they click on a particular point on a map, their location, and these click coords get read into some variables and move onto the next sheet.

Any help would be much appreciated as the things I have seen on internet and googled all seem to be struggling to give me what I need or not work.

Any help is much appreciated.

Cheers
Will
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Looks like a fun project. Running this function will get you the X,Y pixel coordinates of where ever your mouse is when this function is run.
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
 
Public Sub MouseCursorGetOurXY()
        'Following functions will need the above lib "user32" declarations
    l = GetCursorPos(MyPointAPI) 'get our cursor position first
    MsgBox CStr(MyPointAPI.x) & ", " & CStr(MyPointAPI.Y) 'displays cursor X Y coordinates
End Sub
But before that, you will need to know when they clicked that left mouse button on the screen. Likely you will have to integrate it with following type of sub:
Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
If you study about Worksheet_SelectionChange code to learn how to get it to run when the mouse left button is clicked somewhere on the screen, then you could perhaps call the mouse code above to get the X, Y of where they clicked.

Then if it was the coordinates you needed to turn the page, then more code code could be run next to do the page turning etc.

It will take some experimenting with it for sure. I would suggest considering just tackling one small piece at a time to see how it works with using F8 to step through code pieces. That is how I might approach it. Others may have other approaches.

And if you are new to coding and you tackle this one to get it to work, then I would say you will have done quite well in learning some of the logics of coding to having done so.

Have fun!

Chuck
 
Upvote 0
Hi All,

Cheers for your help, managed to get it to read the x and y coords of where they clicked into my data sheet. The only problem is the computer the map is on only runs in 800x600 so the map is not so clear. Is there any way I can have another click event for the same object in the same private sub. I am thinking click once and it will soom in centred on where you clicked, then you click again to decide on point?

The current solution I am testing reads where you click and then takes you to a precut map on another sheet and reads coords from that one for the data.

Is this the best way?

Private Sub poSector_Click()
Dim MousePT As POINTAPI
Dim x As Double, y As Double
GetCursorPos MousePT
x = MousePT.x
y = MousePT.y
If x < 400 Then
If y < 300 Then
Sheets("NW Map").Select
End If
If y > 300 Then
Sheets("SW Map").Select
End If
End If
If x > 400 Then
If y < 300 Then
Sheets("NE Map").Select
End If
If y > 300 Then
Sheets("SE Map").Select
End If
End If
End Sub

If it helps anyone this is the code that was in the Sheet Object for reading the coords:

Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub poSector_Click()
Dim MousePT As POINTAPI
Dim x As Double, y As Double
GetCursorPos MousePT
x = MousePT.x
y = MousePT.y
Sheets("Data").Range("G2").Value = x
Sheets("Data").Range("H2").Value = y
Sheets("Type").Select
End Sub

Cheers
Will
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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