setcursorpos() not working on y axis.

medic5678

Board Regular
Joined
Nov 13, 2018
Messages
67
this should be pretty simple.

Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

I was capturing the mouse position with GetCursorPos, and I noticed that it would always treat the y value as zero.

So I put in manual coordinates, and it still does the same thing.

Sub Setcursor()


SetCursorPos 300, 100


End Sub

whatever I change the x value to (first value) works fine going across the screen horizontally. Vertically, any number I put in is treated as 0.

I'm using this in a userform.
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try replacing
Code:
Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
with
Code:
Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long[COLOR=#ff0000]Ptr[/COLOR], ByVal y As Long[COLOR=#ff0000]Ptr[/COLOR]) As Long[COLOR=#ff0000]Ptr[/COLOR]
 
Upvote 0
The declaration is correct as it was and should not be changed. It also works fine for me as written - what version of Office and Windows are you using?
 
Upvote 0
I don't have that available to me here, but can try it later. Is it 64bit Office as well? (not that it affects the code)
 
Upvote 0
I think I'll create a blank form in a new project and try this to determine if it's something with the environment or some curious interaction with my project.
 
Upvote 0
It works with Windows 10 and Excel 2016 (32 bit) for me
 
Upvote 0
I don't have that available to me here, but can try it later. Is it 64bit Office as well? (not that it affects the code)

yes. Office 2016 64 bit. Thanks for the replies, this tells me I need to experiment a bit more.
 
Upvote 0
The fact that one co-ordinate worked and not the other suggests that the original code is probably ok and that someting else is causing the problem
- I do not understand the mechanics behind the scenes but slavishly follow what Microsoft says
- RoryA is very knowlegeable and is probably correct but did you try my suggestion?

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/longptr-data-type
this is what Microsoft says:
LongPtr is not a true data type because it transforms to a Long in 32-bit environments, or a LongLong in 64-bit environments.
Using LongPtr enables writing portable code that can run in both 32-bit and 64-bit environments. Use LongPtr for pointers and handles
 
Upvote 0
The fact that one co-ordinate worked and not the other suggests that the original code is probably ok and that someting else is causing the problem
- I do not understand the mechanics behind the scenes but slavishly follow what Microsoft says
- RoryA is very knowlegeable and is probably correct but did you try my suggestion?

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/longptr-data-type
this is what Microsoft says:
LongPtr is not a true data type because it transforms to a Long in 32-bit environments, or a LongLong in 64-bit environments.
Using LongPtr enables writing portable code that can run in both 32-bit and 64-bit environments. Use LongPtr for pointers and handles

Trying it now. Thanks for the suggestion. By the lunch I'll know a lot more :). The wife's got me doing chores, so my time is limited until the evening.
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,457
Members
453,042
Latest member
AbdelrahmanExcel

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