To move the mouse cursor with VBA, you call the Windows API function SetCursorPos through a Declare statement. VBA has no built-in mouse control, so you declare the function at the top of a module, then call it with X and Y screen coordinates. That is the whole mechanism: one API call, two integers. Everything else is about getting the coordinates right, keeping the declaration in the correct scope, and knowing the limits of what this approach can and cannot do.
What You'll Need
- A Windows PC running Excel, Access, Word, or any Office application with the VBA editor (Alt+F11). This technique does not work on macOS because the API calls are Windows-specific.
- A macro-enabled workbook (.xlsm) or document if you want to save the code.
- Basic familiarity with the VBA editor: inserting a module and running a macro with F5.
- Permission to run macros in your Office Trust Center settings. If macros are disabled, the code will not execute.
Step-by-Step
- Open the VBA editor. In Excel, press Alt+F11. The editor opens in a separate window.
- Insert a standard module. In the editor menu, choose Insert > Module. A blank code pane appears. Do not paste API declarations into a sheet module or a ThisWorkbook module; they must be in a standard module for the scope to work correctly.
- Declare the API function at the top of the module. Paste this exact block above any Sub:
Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
ThePtrSafekeyword is required in 64-bit Office. Omitting it causes a compile error on modern installations. - Write a Sub that calls the function. Below the declaration, add:
Sub MoveMouse()SetCursorPos 500, 300End Sub
The first number is the X coordinate in pixels from the left edge of the primary screen; the second is Y in pixels from the top. - Run the macro. Place the cursor inside the Sub and press F5, or use Run > Run Sub/UserForm. The pointer jumps to the specified position.
- Read the return value if you need confirmation.
SetCursorPosreturns a non-zero Long on success and zero on failure. You can capture it:Dim result As Long: result = SetCursorPos(500, 300). A zero usually means the coordinates are outside the virtual screen or the call was blocked. - Adjust coordinates for your screen. To find valid values, move the pointer manually and read its position. You can add a second declaration for
GetCursorPosand aPOINTAPItype to retrieve current coordinates, but for a simple move you can also estimate from your screen resolution. Coordinates beyond the screen width or height are clamped or rejected. - Add a pause if you are chaining moves. Consecutive
SetCursorPoscalls execute almost instantly. If you want a visible glide, insertApplication.Wait Now + TimeValue("00:00:01")or aSleepAPI call between positions. Note thatSleeprequires its own declaration fromkernel32.
Common Mistakes
- Missing PtrSafe. On 64-bit Office, a declaration without
PtrSafethrows "The code in this project must be updated for use on 64-bit systems." Add it. - Placing the declaration in the wrong module. API declarations must be at module level, before any procedure. Putting them inside a Sub causes a compile error.
- Using screen coordinates from a different monitor layout. On multi-monitor setups, coordinates can be negative (left of the primary display) or larger than the primary resolution. Test on the actual machine.
- Expecting clicks.
SetCursorPosonly moves the pointer. It does not click, drag, or scroll. Clicking requiresmouse_eventorSendInput, which are separate and more complex declarations. - Forgetting macro security. If the workbook opens with macros disabled, nothing runs. Enable macros for that file or trust the location.
Tips
- Store X and Y in variables or cells so you can change them without editing code.
- Use
GetCursorPosto capture the current position before moving, so you can restore it afterward. - Keep the declaration block in one dedicated module named something like
modMouseto avoid duplicate declarations across modules, which cause "Ambiguous name detected" errors. - If you need smooth movement, loop in small increments with a short delay; a single jump is instant and can be disorienting.
- Test in a throwaway workbook first. Moving the pointer while a macro runs can interfere with other automation.
Troubleshooting
If the pointer does not move, check these in order. First, confirm the macro actually ran: add MsgBox "done" after the call. If the message appears but the pointer stays put, the coordinates are likely off-screen or on a disconnected monitor; try 100, 100. Second, confirm you are on Windows; this API does not exist on macOS. Third, check for a compile error by choosing Debug > Compile VBAProject. Fourth, verify the declaration is at the top of a standard module and includes PtrSafe. Fifth, if you are running inside a Remote Desktop session or a virtual machine, the pointer may be controlled by the host; test locally. Finally, some security software restricts synthetic input; if other macros work but this one does not, that is a possibility to investigate with your IT administrator.
Frequently Asked Questions
Can VBA move the mouse without Windows API calls?
No. VBA has no native mouse object. The Windows API is the standard method.
Does this work on Mac?
No. SetCursorPos is a Windows function. Mac Office does not expose an equivalent through VBA.
Can I make the cursor click as well as move?
Not with SetCursorPos alone. You need additional API calls such as mouse_event or SendInput, declared separately.
Why does the cursor go to the wrong spot on my second monitor?
Coordinates are relative to the primary display's top-left corner. Monitors to the left have negative X values; monitors above have negative Y values. Adjust accordingly.
Is moving the mouse with VBA safe?
It is safe for your own automation. Do not use it to control someone else's computer without permission.
Do I need to enable anything in Trust Center?
Macros must be enabled for the file. No other setting is required for the API call itself.