MrExcel Publishing
Your One Stop for Excel Tips & Solutions

How can I get current position of "cursor"?

Posted by P. P. Ervo on September 04, 2001 8:30 AM

How can I find the actual position of the cursor? I'am working with a macro that needs the actual position of the cursor.

Posted by Sean on September 04, 2001 2:58 PM


Enter a line

curloc = activecell.address

into your macro.

This will return the current cell value as "$A$3" if your cursor was in cell A3.

Hope this helps


Posted by Mark O'Brien on September 06, 2001 2:05 PM

If you need the actual cursor position and not the postion of the highlighted cell here's quick example of the GetCursorPos API call.

First, create a user form and add a commandbutton to it. Then add this code to the userform:

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Dim pos As POINTAPI ' Declare variable

Private Sub CommandButton1_Click()
GetCursorPos pos ' Get Co-ordinates
MsgBox "Cursor Pointer is at:" & vbNewLine _
& "x:=" & pos.x & vbNewLine _
& "y:=" & pos.y
End Sub

Run the userform and when you click on the button you will be told where the cursor is.

Hope this is helpful.