By default, the Microsoft Excel VBA UserForm ListBox does not support the mouse scroll wheel, requiring users to manually drag the scrollbar. To enable this functionality, you must use Windows API (Application Programming Interface) calls to "hook" the mouse events and redirect the scroll message to the ListBox control. This process involves creating a standard module to handle the system-level messages and a UserForm module to initiate and terminate the hook when the form opens and closes.
What You'll Need
- Microsoft Excel with the Developer tab enabled.
- A UserForm containing at least one ListBox control.
- Basic familiarity with the VBA editor (Alt + F11).
- A 32-bit or 64-bit version of Excel (the code provided handles both via conditional compilation).
Step-by-Step
- Open the VBA Editor: Press
Alt + F11to open the Visual Basic for Applications editor. - Insert a Standard Module: Go to
Insert > Module. This is where the heavy lifting happens. You cannot put API declarations inside a UserForm module; they must be in a standard module to remain in scope. - Declare the API Functions: Paste the following code into the standard module. This code uses
PtrSafefor 64-bit compatibility and defines the necessary Windows functions to intercept mouse messages. - Configure the UserForm Initialization: Right-click your UserForm in the Project Explorer and select
View Code. You must tell the program to start "listening" for the mouse wheel when the form loads. Add the following to theUserForm_Initializeevent: - Configure the UserForm Termination: This is the most critical step. If you do not unhook the window procedure when the form closes, Excel will likely crash the next time you interact with the UI. Add this to the
UserForm_Terminateevent: - Test the Functionality: Run the UserForm (F5), hover your mouse over the ListBox, and use the scroll wheel. The
TopIndexproperty is modified by the code, which shifts the visible area of the list.
Option Explicit
#If VBA7 Then
Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
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
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If
Private Const GWL_WNDPROC As Long = -4
Private Const WM_MOUSEWHEEL As Long = &H20A
#If VBA7 Then
Private lpPrevWndProc As LongPtr
Private hWndUserForm As LongPtr
#Else
Private lpPrevWndProc As Long
Private hWndUserForm As Long
#End If
Public Sub HookMouseWheel(ByVal FormCaption As String)
#If VBA7 Then
hWndUserForm = FindWindow("ThunderDFrame", FormCaption)
lpPrevWndProc = SetWindowLongPtr(hWndUserForm, GWL_WNDPROC, AddressOf WindowProc)
#Else
hWndUserForm = FindWindow("ThunderDFrame", FormCaption)
lpPrevWndProc = SetWindowLong(hWndUserForm, GWL_WNDPROC, AddressOf WindowProc)
#End If
End Sub
Public Sub UnHookMouseWheel()
#If VBA7 Then
SetWindowLongPtr(hWndUserForm, GWL_WNDPROC, lpPrevWndProc)
#Else
SetWindowLong(hWndUserForm, GWL_WNDPROC, lpPrevWndProc)
#End If
End Sub
#If VBA7 Then
Private Function WindowProc(ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
#Else
Private Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If
If Msg = WM_MOUSEWHEEL Then
Dim delta As Integer
delta = HIWORD(wParam)
' Target the ListBox. Replace "ListBox1" with your actual control name
On Error Resume Next
If delta > 0 Then
UserForm1.ListBox1.TopIndex = UserForm1.ListBox1.TopIndex - 3
Else
UserForm1.ListBox1.TopIndex = UserForm1.ListBox1.TopIndex + 3
End If
On Error GoTo 0
End If
#If VBA7 Then
WindowProc = CallWindowProc(lpPrevWndProc, hWnd, Msg, wParam, lParam)
#Else
WindowProc = CallWindowProc(lpPrevWndProc, hWnd, Msg, wParam, lParam)
#End If
End Function
Private Function HIWORD(ByVal dw As LongPtr) As Integer
#If VBA7 Then
HIWORD = (dw
& 0xFFFF0000)
/ & 0x10000
#Else
HIWORD = (dw And & 0xFFFF0000)
/ & 0x10000
#End If
End Function
Private Sub UserForm_Initialize()
Call HookMouseWheel(Me.Caption)
End Sub
Private Sub UserForm_Terminate()
Call UnHookMouseWheel
End Sub
Common Mistakes
- Forgetting the UnHook Call: The most common error is omitting the
UnHookMouseWheelcall in theUserForm_Terminateevent. Because the API redirects the system's message flow to a specific memory address in your VBA project, closing the form without resetting that address leaves Windows trying to send messages to a part of memory that no longer exists, leading to an immediate crash of the Excel application. - Incorrect Control Naming: In the
WindowProcfunction, the code explicitly referencesUserForm1.ListBox1. If your form is namedfrmDataand your listbox islstItems, the code will fail silently (due toOn Error Resume Next) or throw an error. You must update these references to match your exact object names. - Placing API Declarations in UserForm Modules: API declarations must be at the top of a standard module. Placing them inside a UserForm module will often result in a "Compile error: Procedure declaration not allowed" or will simply fail to execute the hook correctly.
- Ignoring 64-bit Compatibility: Older tutorials provide code without
PtrSafeandLongPtr. If you use 32-bit code in 64-bit Excel, the project will not compile. Always use conditional compilation (#If VBA7) to ensure the tool works across different Excel installations.
Tips
- Adjusting Scroll Speed: In the
WindowProcfunction, the code usesTopIndex +/- 3. You can change this number to increase or decrease how many items the list jumps per scroll click. A value of 1 is very slow; 5-10 is very fast. - Handling Multiple ListBoxes: If you have multiple ListBoxes on one form, you can add logic to the
WindowProcfunction to determine which ListBox is currently focused. While complex, a simpler way is to create a global variable that tracks the "Active" ListBox and update that variable during theListBox_Enterevent of each control. - Using a Class Module for Scaling: For professional applications, consider wrapping the hook logic in a Class Module. This allows you to apply the scroll wheel functionality to any number of forms without duplicating the API logic in every single project.
- Caption Matching: The
FindWindowfunction relies on theMe.Caption. If you change the caption of your form dynamically while it is open, the hook may lose its reference. Ensure the caption remains constant or update the hook accordingly.
Troubleshooting
If the scroll wheel is not working or Excel is crashing, follow these steps in order:
- Check for Crashes: If Excel crashes immediately after closing the form, you have missed the
UnHookMouseWheelcall in theUserForm_Terminateevent. Restart Excel and add the missing line. - Verify Object Names: Double-check that the name of the UserForm and the ListBox in the
WindowProcfunction exactly match the(Name)property in the Properties window. Case sensitivity matters in some environments. - Confirm the Developer Tab: Ensure that the project is not running in a restricted environment (like a highly locked-down corporate Group Policy) that prevents the execution of Windows API calls.
- Test with a Simple Form: If you have complex logic in your
Initializeevent, create a blank UserForm with one ListBox and the API code. If it works there, the issue is likely a conflict with other code in your main form. - Check Bit-version: Ensure you are using the
#If VBA7block. If you are on a very old version of Excel (pre-2010), thePtrSafekeyword will cause an error; the conditional block handles this, but ensure you haven't deleted the#Elsesections.
Frequently Asked Questions
Will this code work on both Mac and Windows?
No. This solution relies on the user32.dll library, which is a core component of the Windows operating system. macOS has a completely different windowing architecture and does not support these specific API calls.
Can I use this with a ComboBox?
The logic is similar, but ComboBoxes are more complex because they toggle between a text box and a dropdown list. Hooking the dropdown list requires finding the window handle of the dropdown specifically, which is more difficult than hooking the main UserForm frame.
Does this slow down Excel's performance?
No. The overhead of a window hook is negligible. The WindowProc function only executes when a Windows message is sent to the form. Since mouse wheel events are infrequent compared to CPU cycles, you will not notice any performance dip.
Why doesn't Microsoft just include this by default?
The VBA UserForm controls are based on very old legacy libraries (ActiveX). These controls were designed before the mouse wheel became a standard input device, and Microsoft has not updated the underlying architecture of UserForms in many years.
Is it safe to distribute this file to other users?
Yes, provided you use the PtrSafe and LongPtr declarations. Because the code accounts for both 32-bit and 64-bit Excel, it will run on most modern Windows installations without requiring the end-user to change any settings.