2

I am attempting to have the mouse pointer move at the center of the selected cell when navigating from cell to cell with the Arrow keys

In Excel 2010 the following solution works perfectly

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

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

SetCursorPos _
    ActiveWindow.ActivePane.PointsToScreenPixelsX(Target.Left + (Target.Width / 2)), _
    ActiveWindow.ActivePane.PointsToScreenPixelsY(Target.Top + (Target.Height / 2))

End Sub

However in Excel 2003 ActiveWindow.ActivePane does not have the PointsToScreenPixelsX and PointsToScreenPixelsY methods. So I tried to find another solution such as the one below. The X Axis works fine but the Y Axis does not.

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

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

SetCursorPos _
    ActiveWindow.Application.ActiveWindow.PointsToScreenPixelsX((Target.Left + (Target.Width / 2)) / 0.75), _
    ActiveWindow.Application.ActiveWindow.PointsToScreenPixelsY((Target.Top + (Target.Height / 2)) / 0.75)

End Sub

I wish this to work regardless of resolution etc. Any ideas?

Kara
  • 6,115
  • 16
  • 50
  • 57
SGalea
  • 712
  • 9
  • 18
  • Could you be more precise: How does it not work? Also, instead of the `ActiveWindow.Application.ActiveWindow` ping-pong reference, you can just say `ActiveWindow`. – Jean-François Corbett Jun 13 '13 at 08:12
  • The mouse pointer moves to the wrong position not in the current selected cell – SGalea Jun 13 '13 at 08:54
  • A lot of positioning references don't work relative to the point you'd expect (i.e. the offset from the top left corner of the screen). Simple question that might point you in the right direction: if you resize the Excel window, does the code fail in the same way (i.e. with the cursor position being set in the same relative location within the Excel Application window). How about if you move your start menu to be vertical? If either of these has an impact, you may find you need to manually offset the values you're passing by the size of the start menu and/or the position of the Excel instance. – tobriand Jul 20 '13 at 11:47

1 Answers1

2

This should work for older versions of Excel. It's clunky but it gets the job done.

Declare Function SetCursorPos Lib "user32" _
   (ByVal x As Long, ByVal y As Long) As Long
Declare Function GetDC Lib "user32" ( _
  ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" ( _
  ByVal hwnd As Long, ByVal hDC As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" ( _
  ByVal hDC As Long, ByVal nIndex As Long) As Long

Sub MoveMouseToRange(R As Range)
Static lDPI&(1), lDC&

If lDPI(0) = 0 Then
    lDC = GetDC(0)
    lDPI(0) = GetDeviceCaps(lDC, 88&)    'this is the horizontal 
                                         'resolution of the user's screen,
                                         'in DPI
    lDPI(1) = GetDeviceCaps(lDC, 90&)    'vertical
    lDC = ReleaseDC(0, lDC)
End If

Zoom = R.Parent.Parent.Windows(1).Zoom
x = (R.Left + 0.5 * R.Width) * Zoom / 100 / 72 * lDPI(0)
y = (R.Top + 0.5 * R.Height) * Zoom / 100 / 72 * lDPI(1)
SetCursorPos x, y

End Sub
tpkaplan
  • 182
  • 8
  • I personally do not need it anymore and I do not have anywhere to test it. Can someone check it ? – SGalea Nov 30 '15 at 08:58