Consulting

Results 1 to 5 of 5

Thread: Move the mouse cursor in the top left corner of the selected cell.

  1. #1
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location

    Move the mouse cursor in the top left corner of the selected cell.

    Hi all.

    I would like that the mouse cursor has moved into the top left corner of the selected cell.
    I wrote this macro:
    [vba]Option Explicit
    Private Declare Function SetCursorPos Lib "User32" (ByVal x As Long, ByVal y As Long) As Long

    Sub Makro1()
    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[/vba]

    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

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,844
    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:
    [VBA]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[/VBA]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.
    Last edited by p45cal; 02-11-2013 at 03:19 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Quote Originally Posted by p45cal View Post
    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:
    [VBA]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[/VBA]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)

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,844
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Thank you very much p45cal. The use of Activepane was decisive. That's where i was looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •