PDA

View Full Version : [SOLVED:] Move the mouse cursor in the top left corner of the selected cell.



Artik
02-10-2013, 08:08 PM
Hi all.

I would like that the mouse cursor has moved into the top left corner of the selected cell.
I wrote this macro:

Option Explicit
Private Declare Function SetCursorPos Lib "User32" (ByVal x As Long, ByVal y As Long) As Long

Sub Macro1()
Dim lWinLeft As Long
Dim lWinTop As Long
With ActiveWindow
lWinLeft = .PointsToScreenPixelsX(.Selection.Left)
lWinTop = .PointsToScreenPixelsY(.Selection.Top)
End With
SetCursorPos lWinLeft, lWinTop
End Sub

When I select cell A1, it works properly. But when I select another cell, the mouse cursor is not in the top left corner of the selected cell.

What am I doing wrong?

I apologize for my language. I use a translator.

Artik

p45cal
02-11-2013, 03:07 AM
I don't think you're going to have fun.
Google for exactly:
"How to get the Co ordinates of the Excel Cell"
WITH the quote marks. Look for the experts-exchange.com result and click on it and scroll down, down to the bottom and look at the second-to-last message (by zorvek (it starts:"Below is a revised version of what I posted above."), then try:

Sub blah()
myLeft = ActiveWindow.PointsToScreenPixelsX(0) + GetPixelsFromPointsX(Selection.Left) * ActiveWindow.Zoom / 100
myTop = ActiveWindow.PointsToScreenPixelsY(0) + GetPixelsFromPointsY(Selection.Top) * ActiveWindow.Zoom / 100
SetCursorPos myLeft, myTop
End Sub


It worked well with zooms 100% and more, but on zooms less than 100% it got inaccurate vertically.

ps. I tried to post a direct link to the experts-exchange website, but that link didn't include the responses to the question. This way, (via Google) you don't have to subscribe to experts-exchange.

stranno
03-01-2017, 11:08 AM
I don't think you're going to have fun.
Google for exactly:
"How to get the Co ordinates of the Excel Cell"
WITH the quote marks. Look for the experts-exchange.com result and click on it and scroll down, down to the bottom and look at the second-to-last message (by zorvek (it starts:"Below is a revised version of what I posted above."), then try:

Sub blah()
myLeft = ActiveWindow.PointsToScreenPixelsX(0) + GetPixelsFromPointsX(Selection.Left) * ActiveWindow.Zoom / 100
myTop = ActiveWindow.PointsToScreenPixelsY(0) + GetPixelsFromPointsY(Selection.Top) * ActiveWindow.Zoom / 100
SetCursorPos myLeft, myTop
End Sub
It worked well with zooms 100% and more, but on zooms less than 100% it got inaccurate vertically.

ps. I tried to post a direct link to the experts-exchange website, but that link didn't include the responses to the question. This way, (via Google) you don't have to subscribe to experts-exchange.


Can't find "How to get the Co ordinates of the Excel Cell"
But i like to know how to do this (move mouse to activecell)

p45cal
03-01-2017, 12:32 PM
In a standard code module at the top:
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

In the sheet concerned's code module:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'SetCursorPos _
ActiveWindow.ActivePane.PointsToScreenPixelsX(Target.Left), _
ActiveWindow.ActivePane.PointsToScreenPixelsY(Target.Top)
SetCursorPos _
ActiveWindow.ActivePane.PointsToScreenPixelsX(Target.Left + (Target.Width / 2)), _
ActiveWindow.ActivePane.PointsToScreenPixelsY(Target.Top + (Target.Height / 2))
End Sub

stranno
03-01-2017, 02:03 PM
Thank you very much p45cal. The use of Activepane was decisive. That's where i was looking for.