[Excel VBA] How to Enable Multiple Selection in a ListBox and Retrieve All Selected Items

目次

Introduction

UserForm ListBoxes usually allow selecting only one item at a time. However, there are scenarios where you want to allow selecting multiple items at once, such as “adding multiple products to a cart” or “selecting multiple files for processing.”

By configuring the .MultiSelect property of the ListBox, you can enable multiple selection using the Ctrl and Shift keys.

In this article, I will explain the basic and reliable method to enable multiple selection in a ListBox and retrieve data for all selected items using VBA.

Sample Code: Setting Up Multi-Select and Retrieving Values

This macro consists of two parts:

  1. UserForm_Initialize: Sets the ListBox to allow multiple selections when the form loads.
  2. SubmitButton_Click: Retrieves and displays information for all selected items when the “Submit” button is pressed.

UserForm Code

' Event executed when the form is initialized
Private Sub UserForm_Initialize()
    With Me.ItemListBox
        ' Set to display 3 columns
        .ColumnCount = 3
        .ColumnWidths = "50;120;60"
        
        ' Load data from worksheet range
        .List = ThisWorkbook.Worksheets("ItemList").Range("A1:C10").Value
        
        ' Enable multiple selection using .MultiSelect property
        ' fmMultiSelectExtended: Standard multi-select using Shift and Ctrl keys
        .MultiSelect = fmMultiSelectExtended
    End With
End Sub

' Process when the "Submit" button (SubmitButton) is clicked
Private Sub SubmitButton_Click()
    Dim i As Long
    Dim resultText As String
    
    resultText = "[Selected Items]" & vbCrLf
    
    With Me.ItemListBox
        ' Loop through all rows using .ListCount property
        ' Note: ListBox index starts at 0, so loop until Count - 1
        For i = 0 To .ListCount - 1
            ' Check if the i-th row is selected using .Selected(i) property
            If .Selected(i) = True Then
                ' If selected, add that row's data to the result string
                resultText = resultText & _
                             "- " & .List(i, 0) & ", " & _
                             .List(i, 1) & ", " & _
                             .List(i, 2) & " Yen" & vbCrLf
            End If
        Next i
    End With
    
    MsgBox resultText, vbInformation, "Selection Results"
End Sub

Explanation of the Code

.MultiSelect = fmMultiSelectExtended

This single line is the key to enabling multiple selections.

  • .MultiSelect: The property that sets the selection mode of the ListBox.
  • fmMultiSelectExtended: The most common multi-select mode. It allows range selection with the Shift key and individual toggling with the Ctrl key.
  • fmMultiSelectMulti: A mode where clicking an item or pressing the spacebar toggles selection on/off (no Ctrl/Shift needed).
  • fmMultiSelectSingle: The default single-selection mode.

For i = 0 To .ListCount - 1

To retrieve selected items, you must check every row from the beginning to the end of the list.

  • .ListCount: Returns the total number of rows in the list. Since the index starts at 0, the loop runs from 0 to .ListCount - 1.

If .Selected(i) = True Then

This is the core logic for determining if a row is selected.

  • .Selected(i): Returns True if the i-th row is selected, and False otherwise.
  • The If statement ensures that data retrieval processing is performed only for rows where this property is True.

Summary

In this article, I explained how to enable multiple selection in a ListBox and retrieve all selected items.

  • Enable the mode using the .MultiSelect property.
  • Loop through all rows using .ListCount.
  • Check the .Selected(Index) property inside the loop to get data for True rows.

Mastering this procedure allows you to create more advanced and flexible tools where users can batch-process multiple selected data entries.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次