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.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello and welcome to MrExcel.

Try

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

Adjust ranges to suit.
 
Upvote 0
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>
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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>
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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