Announcement

Collapse
No announcement yet.

Unconfigured Ad Widget

Collapse

Getting X Y coordinate of Cursor in VBA

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting X Y coordinate of Cursor in VBA

    Is there a VBA routine the returns the current X Y coordinates of the cursor ?
    Or how do I write a macro to create a drawing object at the current cursor position ?

    Neil P

  • #2
    Re: Getting X Y coordinate of Cursor in VBA

    Hi Neil,

    Can you explain a little more on what you are trying to do.

    It maybe possible to get the values but I think the type of units (points/twips/pixels) returned varys.

    Cheers
    Andy

    Comment


    • #3
      Re: Getting X Y coordinate of Cursor in VBA

      Hi Andy,

      Thanks for responding.

      I have a set of macros that automatically create drawing objects in worksheet - lines, rectangles, text boxes etc. I want to be able to move the cursor over part of the drawing and have a macro that automatically creates a drawing object at the location of the cursor (eg I might use the OnKey event to draw a rectangle at the cursor location if the user hits the R key).

      I havent been able to find any property or function that returns the current XY position of the cursor relative to the top left hand corner of the worksheet (not the screen) - in any units.

      Thanks for your help on this,

      Neil

      Comment


      • #4
        Re: Getting X Y coordinate of Cursor in VBA

        Hi,

        This is going to take some pretty complex API code, which I don't have to hand.

        Here is an example of getting the Cursors X and Y values in pixels.
        Code:
        Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
        Type POINTAPI
            x As Long
            y As Long
        End Type
        
        Sub CurosrXY_Pixels()
            
            Dim lngStatus As Long
            Dim typWhere As POINTAPI
            
            lngStatus = GetCursorPos(typWhere)
            MsgBox "x: " & typWhere.x & Chr(13) & "y: " & typWhere.y, vbInformation, "Pixels"
        
        End Sub
        You will need to determine the position of the Excel application on the desktop, the workbooks position within the application in order to start the shape at the cursors position.

        I don't know how comfortable you are with this type of VBA coding.
        It's not what I usually do for sure!

        It may be possible to tackle this from a different angle if you can share an example workbook.
        Have a look at my clocks example. Especially the one that follows the cursor around.

        Cheers
        Andy

        Comment


        • #5
          Re: Getting X Y coordinate of Cursor in VBA

          Thanks Andy !!. I'll give that a try.

          Comment

          Trending

          Collapse

          There are no results that meet this criteria.

          Working...
          X