Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA UserForm Mouse Wheel Scrolling Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To make a VBA UserForm respond to the mouse wheel, you need a Windows API hook that captures wheel messages and routes them to the control under the pointer. Excel VBA has no built-in wheel event for UserForms, so the reliable approach is to subclass the form's window using AddressOf and SetWindowLong, then handle WM_MOUSEWHEEL in a callback that scrolls a ScrollBar, ListBox, or Frame. This guide walks through the working pattern, the pitfalls that break it, and how to test it safely.

What You'll Need

  • Excel on Windows (the API calls below are Windows-only; Excel for Mac does not support this technique).
  • A UserForm with at least one scrollable control (ListBox, ScrollBar, or a Frame acting as a scroll container).
  • Access to the VBA editor: press Alt+F11, then insert a module and a UserForm.
  • Macro security set to allow macros in the workbook (File > Options > Trust Center > Trust Center Settings > Macro Settings). Labels vary slightly by Excel version.
  • A 32-bit or 64-bit declaration set that matches your Office installation. The PtrSafe keyword is required in 64-bit Office.

Step-by-Step

  1. Insert a standard module. In the VBA editor, choose Insert > Module. Paste the API declarations there so they are visible to the UserForm code. Use conditional compilation so the same file works on both bitnesses:
    #If VBA7 Then
        Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
        Private Declare PtrSafe Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As LongPtr, ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
        Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
    #Else
        Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
        Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    #End If
    Private Const GWL_WNDPROC As Long = -4
    Private Const WM_MOUSEWHEEL As Long = &H20A
  2. Add a module-level variable to hold the original window procedure address, for example Private mPrevWndProc As LongPtr (use Long in 32-bit code). This lets you restore the original handler when the form closes.
  3. Write the callback function. It must have the signature Public Function WheelProc(ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr (adjust types for 32-bit). Inside, check If Msg = WM_MOUSEWHEEL Then and read the wheel delta from the high word of wParam. A positive delta means scroll up; negative means down. Route the value to your control, then return 0 to swallow the message. Otherwise call CallWindowProc mPrevWndProc, hWnd, Msg, wParam, lParam so normal form behaviour continues.
  4. Hook the form when it loads. In the UserForm's UserForm_Initialize event, call SetWindowLongPtr Me.hWnd, GWL_WNDPROC, AddressOf WheelProc and store the returned value in mPrevWndProc. The Me.hWnd property gives the form's window handle.
  5. Unhook when the form closes. In UserForm_Terminate, call SetWindowLongPtr Me.hWnd, GWL_WNDPROC, mPrevWndProc. Skipping this leaves a dangling pointer and can crash Excel the next time the form opens.
  6. Scroll the intended control. For a ListBox, change ListIndex or TopIndex by a small step per wheel notch (for example 3 lines). For a ScrollBar, adjust Value within Min and Max. For a Frame, move the Top property of the child controls or use a hidden ScrollBar as the driver. Clamp values so you never scroll past the first or last item.
  7. Test with the form shown. Press F5 in the VBA editor to run the form, then roll the wheel over the control. If nothing happens, confirm the hook ran (add a Debug.Print in the callback) and that the form has focus.

Common Mistakes

  • Using Long declarations in 64-bit Office, which causes a "Type mismatch" or silent failure. Always include PtrSafe and LongPtr for 64-bit.
  • Forgetting to restore the original window procedure in UserForm_Terminate, which can crash Excel.
  • Putting the API declarations in the UserForm module instead of a standard module; AddressOf requires a standard module.
  • Assuming the wheel message arrives at the control. It arrives at the form, so you must decide which control to scroll based on the cursor position or the focused control.
  • Testing on Excel for Mac, where these Windows API calls do not exist.

Tips

  • Use GetCursorPos and ScreenToClient to find which control is under the pointer before scrolling, so multiple list boxes behave naturally.
  • Keep the scroll step small (2–3 items per notch) for a smooth feel; larger steps feel jumpy.
  • Wrap the hook in error handling so a failed API call shows a message instead of crashing Excel.
  • Save your workbook before testing; a bad hook can force Excel to close.

Troubleshooting

If the wheel does nothing, first check that the callback is actually being called by adding a temporary Debug.Print Msg and watching the Immediate Window. If no message appears, the hook did not install: verify Me.hWnd is valid and that SetWindowLongPtr returned a non-zero value. If the form scrolls but the wrong control moves, add cursor-position logic. If Excel crashes on closing the form, you almost certainly missed the unhook step. If you get a compile error on PtrSafe, your Office is 32-bit and you should use the #Else branch. If the form works once and then stops, the original procedure was not restored and the hook is stale.

Frequently Asked Questions

Does this work on Excel for Mac?

No. The Windows API calls used here are not available on Mac, and VBA on Mac has no equivalent wheel hook.

Can I scroll a Frame instead of a ListBox?

Yes, but a Frame has no built-in scroll. You move the child controls' Top values or drive a hidden ScrollBar from the wheel callback.

Why does the form crash when I close it?

The original window procedure was not restored. Add the SetWindowLongPtr call in UserForm_Terminate to put it back.

Do I need PtrSafe?

Yes, in 64-bit Office. Without it the code will not compile or will fail at runtime.

Can I use this in Excel 365?

Yes, the technique works in current Windows versions of Excel; menu labels may differ slightly by version.