How to place the pointer on a specific cell

edany

New Member
Joined
Jul 24, 2009
Messages
7
Is someone knows how to place the mouse’s pointer at specific location on the worksheet, at the end of macro run?
I would like to add VBA code in macro of a Commad Button, that can do that.
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
Hello and welcome to MrExcel.

Try

Code:
Application.Goto reference:=Sheets("Sheet3").Range("E5"), scroll:=True
Adjust ranges to suit.
 

edany

New Member
Joined
Jul 24, 2009
Messages
7
Thanks VoG for trying to help. I have copied the suggested code to a Command Button’s macro and clicked that button. The suggested macro had selected cell E5 on sheet 3 but the pointer of the mouse still stayed at its place pointing on the Command Button that I clicked. I would like that at the end of this macro run the pointer of the mouse will point and will be placed on cell E5.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
 

edany

New Member
Joined
Jul 24, 2009
Messages
7
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
 

wsjackman

Well-known Member
Joined
Jun 19, 2008
Messages
2,465
changing the mouse position is not an excel issue.

It will probably need to be someone who knows the details of your computer, mouse, and operating system to write machine code to change the mouse position as well as knowing how your excel display "Fits" in the screen.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
I'm not sure how to force the cursor to move. Try this then type something - what you type will appoear in the selected cell:

Code:
Private Sub CommandButton1_Click()
Application.Goto reference:=Sheets("Sheet3").Range("E5"), Scroll:=True
ActiveCell.Select
End Sub
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,686
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
You can use the SetCursorPos API to move the cursor but determining the correct coordinates may take some work. Can I ask why you want to do this?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,686
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Assuming the cell is visible on screen something like this:
Code:
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpszClass As String, ByVal lpszTitle As String) As Long
'**Win32 API User Defined Types
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Sub MoveCursorTo(rngTarget As Range)
    Dim wks As Worksheet, objCht As ChartObject
    Dim lngChtHwnd As Long, lngXLDesk As Long
    Dim rctPos As RECT
    With rngTarget
        Set wks = .Parent
        Set objCht = wks.ChartObjects.Add(.Left, .Top, 0, 0)
    End With
    objCht.Activate
    lngXLDesk = FindWindowEx(Application.hwnd, 0&, "XLDESK", vbNullString)
    lngChtHwnd = FindWindowEx(lngXLDesk, 0&, "EXCELE", vbNullString)
    GetWindowRect lngChtHwnd, rctPos
    SetCursorPos rctPos.Left, rctPos.Top
    objCht.Delete
End Sub
Sub test()
    MoveCursorTo Range("AA17")
End Sub
 

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,198
As an example, this will place the cursor over the top left corner of cell B2, stick it into a new fresh standard module.

Code:
Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
 
Sub SetCursorUpperLeftCorner()
Dim X&, Y&
With ActiveWindow
X = .PointsToScreenPixelsX((Range("B2").Left) * 1.38888888888889 * .Zoom / 100)
Y = .PointsToScreenPixelsY((Range("B2").Top) * 1.38888888888889 * .Zoom / 100)
End With
SetCursorPos X, Y
End Sub
 

edany

New Member
Joined
Jul 24, 2009
Messages
7
Answering rorya's question, I have two same sizes command buttons placed on the same location on the worksheet. Last macro code of each button ends with selecting the button and “Send to Back” (Draw, Order), each time one button is visible on top of the other that was previously sent to back, something like toggle action.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
When the visible button is clicked the pointer of mouse stays on the button. When the macro finishes to run the button doesn’t go backward unless I move the mouse’s pointer off the button.<o:p></o:p>
 

Forum statistics

Threads
1,089,209
Messages
5,406,858
Members
403,109
Latest member
gamer527

This Week's Hot Topics

Top