Fix the GetCursorPos Error in 64-Bit Excel VBA
Question details
The user needs to fix an Excel VBA error involving the POINTAPI type and the GetCursorPos function.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A VBA macro uses the Windows User32 GetCursorPos function to obtain the cursor position.
- Observed behavior
- Excel reports that a public user-defined type cannot be defined within an object module.
This solution uses the Windows User32 API and applies to Excel for Windows. Open the workbook in Excel and press Alt+F11 to locate the existing POINTAPI and GetCursorPos code.
Move POINTAPI and GetCursorPos to a standard VBA module
Use a standard module to resolve the public type error and a conditional declaration to support both 64-bit and older 32-bit Excel VBA.
A public user-defined type must be declared in a standard module, not in a worksheet or ThisWorkbook object module. The POINTAPI coordinates and the function's return value remain Long values in 64-bit VBA.
Press Alt+F11 to open the VBA editor. Select the workbook in Project Explorer, then choose Insert > Module.
Remove the existing POINTAPI type, GetCursorPos declaration, and demo procedure from the object module. Paste this code into the new standard module: Option Explicit Public Type POINTAPI Xcoord As Long Ycoord As Long End Type #If VBA7 Then Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long #Else Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long #End If Public Sub GetCursorPosDemo() Dim pt As POINTAPI If GetCursorPos(pt) <> 0 Then MsgBox "X Position: " & pt.Xcoord & vbNewLine & "Y Position: " & pt.Ycoord Else MsgBox "Could not retrieve the cursor position." End If End Sub
In the VBA editor, choose Debug > Compile VBAProject. Return to Excel, press Alt+F8, select GetCursorPosDemo, and click Run.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work. It does not fix Microsoft Excel's VBA compiler error; check VBA feature availability before relying on macros.

Frequently Asked Questions
Why does POINTAPI cause an error in a worksheet module?
A public user-defined type cannot be declared in a worksheet or ThisWorkbook object module. In the VBA editor, choose Insert > Module and put the POINTAPI declaration in that standard module.
Do I need PtrSafe for GetCursorPos in 64-bit Excel?
Yes. A Windows API Declare statement needs PtrSafe in 64-bit Office. Use #If VBA7 Then with a PtrSafe declaration and a legacy declaration in the #Else branch for older VBA versions.
Should the POINTAPI coordinates be changed to LongPtr?
No. The Windows POINT structure contains two 32-bit coordinates, so both fields should remain Long. The GetCursorPos return value is also a 32-bit value represented by Long.
Will this GetCursorPos code work on a Mac?
No. The declaration calls the Windows User32 library, which is not available in Excel for Mac. A Mac-specific approach would be required.




