Excel VBA MsgBox Position: How to Move It
In this article
Excel VBA's built-in MsgBox function does not let you set where the dialog appears—it always opens centered on the Excel window or screen. To control the position, you need to call the Windows MessageBox API directly with a hook, or use a custom UserForm. This guide shows both methods, starting with the simplest working approach.
What You'll Need
- Excel with macros enabled (the desktop version for Windows; the API method does not work on Mac).
- Access to the VBA editor (Alt+F11).
- Basic familiarity with pasting code into a module.
Step-by-Step
- Open the VBA editor. Press Alt+F11 in Excel. In the Project Explorer, right-click your workbook, choose Insert > Module. This creates a standard module where the code will live.
- Paste the API declarations. At the top of the module, add the following (this uses the Windows API, so it only runs on Windows):
Note: thePrivate Declare PtrSafe Function MessageBox Lib "user32" Alias "MessageBoxW" _ (ByVal hWnd As LongPtr, ByVal lpText As LongPtr, ByVal lpCaption As LongPtr, _ ByVal uType As Long, ByVal wLanguageId As Long) As Long Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _ Alias "SetWindowsHookExW" (ByVal idHook As Long, ByVal lpfn As LongPtr, _ ByVal hmod As LongPtr, ByVal dwThreadId As Long) As LongPtr Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" _ (ByVal hhk As LongPtr) As Long Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long Private Declare PtrSafe Function CallNextHookEx Lib "user32" _ (ByVal hhk As LongPtr, ByVal nCode As Long, ByVal wParam As LongPtr, _ ByVal lParam As LongPtr) As LongPtr Private Declare PtrSafe Function GetWindowRect Lib "user32" _ (ByVal hWnd As LongPtr, ByRef lpRect As RECT) As Long Private Declare PtrSafe Function MoveWindow Lib "user32" _ (ByVal hWnd As LongPtr, ByVal x As Long, ByVal y As Long, _ ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Const WH_CBT As Long = 5 Private Const HCBT_ACTIVATE As Long = 5 Private hHook As LongPtrPtrSafekeyword is required for 64-bit Office; it also works in 32-bit Office. If you are on a very old version, removePtrSafeand changeLongPtrtoLong. - Add the hook callback and the positioning function. Below the declarations, paste:
This sets the hook before the message box appears, then moves the window to coordinates (200,150) when it activates. Adjust X and Y in thePublic Function MsgBoxAt(Prompt As String, Title As String, _ X As Long, Y As Long, Optional Buttons As Long = vbOKOnly) As Long Dim hWndMsg As LongPtr Dim r As RECT Dim tID As Long tID = GetCurrentThreadId() hHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, 0, tID) hWndMsg = MessageBox(0, StrPtr(Prompt), StrPtr(Title), Buttons, 0) UnhookWindowsHookEx hHook MsgBoxAt = hWndMsg End Function Private Function HookProc(ByVal nCode As Long, ByVal wParam As LongPtr, _ ByVal lParam As LongPtr) As LongPtr If nCode = HCBT_ACTIVATE Then Dim r As RECT GetWindowRect wParam, r MoveWindow wParam, 200, 150, r.Right - r.Left, r.Bottom - r.Top, 1 End If HookProc = CallNextHookEx(hHook, nCode, wParam, lParam) End FunctionMoveWindowcall to change the position. - Call the function from a macro. In a new sub, write:
Run it with F5. The dialog should appear at the coordinates you specified, relative to the screen (not the Excel window).Sub ShowAtPosition() MsgBoxAt "Hello from a positioned message box", "Test", 400, 300 End Sub - Test and adjust. If the box appears but not where expected, check your screen scaling. Windows display scaling (e.g., 125% or 150%) can offset pixel coordinates. Use larger values or calculate based on
Application.UsableWidthandApplication.UsableHeightif you need relative placement.
Common Mistakes
- Forgetting
PtrSafeon 64-bit Office. Without it, the code fails to compile with a "Type mismatch" or "The code in this project must be updated for use on 64-bit systems" error. Always includePtrSafe. - Using the hook without unhooking. If you don't call
UnhookWindowsHookEx, the hook stays active and can crash Excel or affect other windows. The sample above unhooks after the message box closes. - Assuming it works on Mac. The Windows API calls are not available on macOS. On Mac, use a custom UserForm instead (see Tips).
- Passing a string variable directly to the API. The API expects a pointer; use
StrPtr()as shown, or declare parameters asByVal LongPtrand passStrPtr. - Ignoring screen resolution changes. Hard-coded pixel coordinates may land off-screen on different monitors. Consider clamping values to the usable area.
Tips
- For a simpler, cross-platform alternative, create a UserForm with a label and buttons, then set its
StartUpPositionproperty to0 - Manualand assignLeftandTopin theInitializeevent. This gives full control without API calls. - If you only need to center the box on the Excel window, the default behavior already does that; the API method is for custom placement.
- Wrap the API calls in error handling to avoid leaving a hook installed if something fails.
- Use
Application.UsableWidthandApplication.UsableHeightto compute positions relative to the Excel window rather than the screen.
Troubleshooting
If the message box does not appear at all, check that macros are enabled and that you are running Windows. If you get a compile error about AddressOf, ensure the callback function is in a standard module (not a class module) and is declared Private. If the box appears but flashes or moves oddly, the hook may be firing multiple times; guard with a flag or check that nCode = HCBT_ACTIVATE only once. If Excel crashes after running, you likely forgot to unhook; restart Excel and fix the code. For persistent issues, test with a minimal macro that calls MsgBoxAt with fixed coordinates.
Frequently Asked Questions
Can I position the standard VBA MsgBox without API calls?
No. The built-in MsgBox function has no position parameter. You must use the Windows API or a custom UserForm.
Does this work on Excel for Mac?
No. The Windows API declarations are Windows-only. Use a UserForm with manual positioning instead.
Why does my message box appear in the wrong place?
Coordinates are in pixels relative to the screen, and Windows display scaling can shift them. Try larger values or compute based on screen dimensions.
Is using Windows API calls safe?
Yes, when used correctly. Always unhook after the message box closes to avoid instability. Test in a copy of your workbook first.
Can I set the position relative to the Excel window instead of the screen?
Yes. Get the Excel window position with Application.Left and Application.Top, then add offsets before calling MoveWindow.