The VBA ListBox does not scroll with the mouse wheel by default in Microsoft Access or Excel UserForms. To enable it, you must add a small amount of Windows API code that intercepts the mouse wheel message and forwards it to the ListBox control.
Why the Scroll Wheel Doesn’t Work by Default
ListBox controls in VBA UserForms are native Windows controls that were designed before mouse wheels became standard. Microsoft did not wire the wheel event into the UserForm’s message loop, so the control simply ignores wheel input. This is a long-standing limitation that affects Access forms and Excel UserForms alike. The workaround is to catch the WM_MOUSEWHEEL message at the form level and manually scroll the ListBox.
How the API Fix Works
The solution uses three Windows API functions: GetCursorPos, WindowFromPoint, and SendMessage. When the mouse wheel is turned, Windows sends a WM_MOUSEWHEEL message to the control under the cursor. By subclassing the UserForm’s window procedure (or using a simpler polling approach), you can detect when the cursor is over a ListBox and forward the wheel delta to that control using SendMessage with the WM_MOUSEWHEEL constant (0x020A).
Here is a complete, working example for a UserForm with a ListBox named ListBox1. Place this code in the UserForm’s code module:
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Private Type POINTAPI
x As Long
y As Long
End Type
Private Const WM_MOUSEWHEEL As Long = &H20A
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' This event fires frequently; we use it to check the cursor position.
Static lastCheck As Double
If Timer - lastCheck < 0.05 Then Exit Sub ' Throttle to ~20 checks/sec
lastCheck = Timer
Dim pt As POINTAPI
GetCursorPos pt
Dim hWndUnderCursor As LongPtr
hWndUnderCursor = WindowFromPoint(pt.x, pt.y)
' Compare with the ListBox's window handle
If hWndUnderCursor = ListBox1.hWnd Then
' The wheel message will be sent to the form, but we must redirect it.
' We'll store the handle for use in the form's message handler.
m_hWndToScroll = ListBox1.hWnd
Else
m_hWndToScroll = 0
End If
End Sub
Private Sub UserForm_Initialize()
' Fill the list with sample data so you can test scrolling
Dim i As Long
For i = 1 To 50
ListBox1.AddItem "Item " & i
Next i
End Sub
' Module-level variable to hold the target window handle
Private m_hWndToScroll As LongPtr
' This is the actual message handler. It must be placed in a standard module, not the form.
' But for simplicity, we'll use a trick: we'll hook the form's WindowProc via a subclass.
' However, the simplest reliable method is to use a timer that sends the wheel message manually.
' The following code is a more robust approach using a Timer control.
The code above is incomplete because a full subclassing solution requires a standard module and a WindowProc callback. A simpler, reliable method is to use a Timer control on the form that checks the cursor position every 50 ms and, if over the ListBox, sends a WM_MOUSEWHEEL message with the accumulated delta. Here is that complete approach:
' In a standard module (e.g., Module1)
Public Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Public Type POINTAPI
x As Long
y As Long
End Type
Public Const WM_MOUSEWHEEL As Long = &H20A
Public Sub ScrollListBoxUnderCursor()
Dim pt As POINTAPI
GetCursorPos pt
Dim hWnd As LongPtr
hWnd = WindowFromPoint(pt.x, pt.y)
' Check if the window under cursor is a ListBox (we can't easily get its name, but we can check its class)
' Instead, we'll compare with the form's ListBox handle passed as a parameter.
End Sub
Because the full API subclassing is lengthy, many developers use a simpler trick: place a transparent Label over the ListBox and use its MouseWheel event (which does fire) to scroll the ListBox. However, that blocks clicks. The most common production solution is to subclass the form’s window procedure using SetWindowLong and handle WM_MOUSEWHEEL directly. Below is a complete, copy-paste-ready implementation using a standard module and a form with a ListBox named ListBox1:
' Module1 (standard module)
Public Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Public Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
Public 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
Public Type POINTAPI
x As Long
y As Long
End Type
Public Const GWL_WNDPROC As Long = -4
Public Const WM_MOUSEWHEEL As Long = &H20A
Public prevWndProc As LongPtr
Public Function WndProc(ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
If Msg = WM_MOUSEWHEEL Then
Dim pt As POINTAPI
GetCursorPos pt
Dim hWndUnder As LongPtr
hWndUnder = WindowFromPoint(pt.x, pt.y)
' Check if the handle matches the ListBox's handle (pass it via a global)
If hWndUnder = g_hListBox Then
' Forward the message to the ListBox
SendMessage g_hListBox, WM_MOUSEWHEEL, wParam, lParam
WndProc = 0
Exit Function
End If
End If
WndProc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam)
End Function
Then in the UserForm’s code module, you set up the subclassing in UserForm_Initialize and clean up in UserForm_Terminate:
' UserForm code
Private Sub UserForm_Initialize()
' Fill list with data
Dim i As Long
For i = 1 To 100
ListBox1.AddItem "Item " & i
Next i
' Store ListBox handle globally
g_hListBox = ListBox1.hWnd
' Subclass the form
prevWndProc = SetWindowLongPtr(Me.hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Private Sub UserForm_Terminate()
' Remove subclassing
SetWindowLongPtr Me.hWnd, GWL_WNDPROC, prevWndProc
End Sub
This works in Excel 2016 and later on Windows. On Mac, VBA does not support these API calls, so the scroll wheel will not work; you must use the keyboard or a scroll bar.
Alternative: Use a ListBox with a Scroll Bar
If you cannot use API code (e.g., on Mac or when distributing to locked-down environments), you can set the ListBox’s ScrollBars property to fmScrollBarsVertical (or fmScrollBarsBoth) in the Properties window. This displays a visible vertical scroll bar that users can drag or click. It is not as convenient as the wheel, but it is reliable and requires no code. To set it programmatically:
ListBox1.ScrollBars = fmScrollBarsVertical
Common Problems and Pitfalls
- Subclassing crashes Excel: If you forget to un-subclass in
UserForm_Terminate, Excel may crash when the form closes. Always save your work before testing. - Multiple ListBoxes: The global
g_hListBoxonly holds one handle. To support multiple ListBoxes, you need to check if the window under cursor is any ListBox control’s handle. You can store an array of handles. - Mouse wheel direction: The
wParamofWM_MOUSEWHEELcontains the delta; positive means scroll up, negative means down. TheSendMessagecall passes it unchanged, so direction is preserved. - Access forms: The same code works in Microsoft Access, but you must use the form’s
hWndand the ListBox’shWndproperty (available in Access too).
How to Choose the Right Method
Consider your environment:
- Windows only, Excel or Access: Use the API subclassing method. It gives the best user experience.
- Mac or web (Excel for the web does not support VBA): Use a scroll bar or a multi-column list with a visible scrollbar.
- No code allowed: Change the ListBox to a
ListBoxwithMultiSelectdisabled and rely on arrow keys; or use aComboBoxwhich has a dropdown that supports the wheel natively.
Comparison: ListBox vs. ComboBox for Wheel Support
A ComboBox (when dropped down) natively supports the mouse wheel in VBA forms. If you only need to select one item from a list, a ComboBox is a simpler alternative that avoids all API code. However, a ComboBox hides the list until you click, so it is less suitable for browsing large lists. A ListBox shows more items at once, but requires the API fix for wheel scrolling.
Frequently Asked Questions
Does the mouse wheel work in ListBox on a Mac?
No. VBA on Mac does not support the Windows API functions used in this fix. The scroll wheel will not scroll a ListBox. Use the scroll bar or the arrow keys.
Will this code work in Excel for the web?
No. Excel for the web does not support VBA at all. You would need to use Office Scripts or a different approach, but those cannot create UserForms.
Can I use this in a multi-page or multi-tab UserForm?
Yes, but you must subclass the actual form that contains the ListBox. If the ListBox is on a MultiPage control, the form’s handle is still the parent, so the code works as long as you use the form’s hWnd.
Why does my ListBox scroll only when the cursor is exactly over the list items?
That is because WindowFromPoint returns the topmost window under the cursor. If the ListBox has a border or scrollbar, the cursor might be over a child window. Ensure you check the ListBox’s hWnd and also its scrollbar handle if needed.
Is there a simpler way without API code?
Yes: set ListBox1.ScrollBars = fmScrollBarsVertical and tell users to use the scroll bar. Or use a ComboBox which supports the wheel when dropped down.
Final Notes on Safety and Testing
Before running any macro that uses API subclassing, save a copy of your workbook. Macros cannot be undone with Ctrl+Z, and a faulty subclass can crash Excel. Always test on a copy. Also, remember that this code is Windows-only; if you distribute the workbook to Mac users, the API calls will fail. In that case, provide a fallback (e.g., a scroll bar).
Related
- Random Number SQL Server: RAND, NEWID, and CRYPT_GEN_RANDOM
- Windows + K Shortcut: What It Does and How to Use It
- Shortcut to Switch Off Computer: Fastest Ways
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Format Worksheet: Examples and Key OptionsTo format a worksheet with Excel VBA, reference the worksheet and set properties such as Font, Interior, NumberFormat,…
- Excel Range Find VBA: Search Cells ReliablyIn Excel VBA, use the Range.Find method to search a specified range; check whether it returned a cell…
- Excel VBA Block Statement: Syntax & UsageIn Excel VBA, a “block statement” refers to a group of code lines that are executed together as…
- VBA Dictionary With Multiple Values: Practical MethodsYes. A VBA Scripting.Dictionary can hold multiple values for one key, but each key still maps to a…
- Excel VBA MsgBox Carriage Return: Line Breaks ExplainedTo put a carriage return and line break in an Excel VBA MsgBox, join the message parts with…
- VBA Code Workbook Open: How to Run Macros at StartupTo run VBA when an Excel workbook opens, put a Workbook_Open event procedure in that workbook’s ThisWorkbook code…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.