logo
search
VBA & Macro Problems

How to Scroll an Excel VBA ListBox to the First Selected Item

Anum MushtaqAnum Mushtaq Oct 14, 2026 869 views

Question details

The user needs to automatically scroll a VBA UserForm ListBox to display the first selected item when the form opens.

How to Scroll an Excel VBA ListBox to the First Selected Item
Product
Excel
Device & OS
not provided
Scenario
Working with a UserForm ListBox in Excel VBA where multiple items are pre-selected via code before the form is displayed to the user.
Observed behavior
The ListBox successfully selects the specified items, but the control does not automatically scroll down, leaving the selected items out of the visible area.
Before you start

Ensure you have access to the VBA Editor (press ALT + F11) in your spreadsheet and that your UserForm with the target ListBox is properly initialized.

Solution 1Recommended

Use the TopIndex Property to Scroll to the First Selected Item

Identify the index of the first selected item in your ListBox and assign it to the TopIndex property to automatically scroll the view.

The TopIndex property dictates which item is displayed at the topmost position of the visible ListBox area. By finding the first selected item programmatically and passing its index to TopIndex, you force the ListBox to scroll directly to that selection.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and locate your UserForm in the Project Explorer.

2
Locate the ListBox Initialization Code

Double-click the UserForm and find the UserForm_Initialize event or the specific macro where your ListBox items are being populated and selected.

3
Loop Through ListBox Items

Add a For loop to iterate through the ListBox items (e.g., For i = 0 To ListBox1.ListCount - 1) and check if each item is selected using the Selected(i) property.

4
Set the TopIndex Property

Inside the loop, when you find the first item where Selected(i) is True, set ListBox1.TopIndex = i, and then use Exit For to stop the loop from overwriting the TopIndex with subsequent selections.

Use the TopIndex Property to Scroll to the First Selected Item
Zero-Based Indexing: By default, VBA ListBox items use zero-based indexing (the first item is index 0). Make sure to adjust your loop logic if you are utilizing a custom one-based configuration array to populate the list.

Run and Edit VBA Macros Seamlessly in WPS Spreadsheet

WPS Office provides robust support for Excel VBA macros. You can easily open, edit, and run your VBA UserForms and ListBox controls directly within WPS Spreadsheet without changing your coding habits.

  1. 1. Open Your File in WPS: Download and install WPS Office, then open your macro-enabled spreadsheet (.xlsm) in WPS Spreadsheet.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon menu. If it is hidden, you can enable it in the Options menu.
  3. 3. Launch the VBA Editor: Click the 'Visual Basic' icon or press ALT + F11 to open the WPS Macro Editor.
  4. 4. Apply TopIndex Code: Double-click your UserForm, insert the TopIndex logic into your ListBox loop, and run the macro to see the auto-scroll in action.
Fully compatible with Microsoft Excel macro formats (.xlsm and .xlsb).Includes a built-in VBA Editor for modifying UserForms, modules, and scripts.Lightweight and fast, ensuring smooth performance for complex macro calculations.Free and easy-to-use interface that requires zero learning curve for Excel users.
microsoft office alternative - wps office

Frequently Asked Questions

What is the TopIndex property in a VBA ListBox?

The TopIndex property determines which item appears at the very top of the visible portion of a ListBox. Modifying this property allows you to programmatically scroll the ListBox to a specific item without requiring the user to scroll manually.

Why is my VBA ListBox index selecting the wrong item?

VBA ListBox indices are zero-based by default, meaning the very first item is at index 0. If your code calculates the index based on a one-based count (starting at 1), it will result in an offset error. Ensure your loops start at 0 or adjust the calculation accordingly.

Can I select multiple items without scrolling to them?

Yes. If you simply set the Selected property of multiple items to True without modifying the TopIndex property, the ListBox will silently select those items in the background but remain scrolled to its original position.