You are on page 1of 4

The VBA Guide For Using Userform ListBox Controls

https://www.thespreadsheetguru.com/blog/vba-useform-listbox-control-guide

1. Add An Item to The ListBox


1.1. To The End
ListBox1.AddItem "Item 1"
ListBox1.AddItem "Item 2"
ListBox1.AddItem "Item 3"
ListBox1.AddItem "Item 4"
ListBox1.AddItem "Item 5"
ListBox1.AddItem "Item 6"

1.2. To A Specific Position


Remember ListBoxes are zero based, so the first item is really at position 0. So if you want to add an item to the 5th position, you
need to reference number 4 in the AddItem function.
'Add to the 2nd Position (subtract 1 from desired)
ListBox1.AddItem "Apple", 1

2. Add Multiple Items To The ListBox


2.1. Individually Written
Sub ListBox_Load()

ListBox1.AddItem "Apple"
ListBox1.AddItem "Orange"
ListBox1.AddItem "Pear"

End Sub

2.2. From An Array List


Sub ListBox_LoadArray()
Dim myArray As Variant

myArray = Array("Apple", "Orange", "Pear")


ListBox1.List = myArray

End Sub

2.3. From A Cell Range


Sub ListBox_LoadRange()
Dim cell As Range

'Load to ListBox
  For Each cell In Worksheets("Sheet1").Range("A1:A6")
    ListBox1.AddItem cell.Value
  Next cell

End Sub

2.4. From A Table Object (ListObject)


Sub ListBox_LoadTable()
Dim tbl As ListObject
Dim cell As Range
'Store Table Object to a variable
    Set tbl = Sheet1.ListObjects("Table1")

'Load List Box


  For Each cell In tbl.DataBodyRange.Columns(1).Cells
    ListBox1.AddItem cell.Value
  Next cell
  
End Sub

3. Delete ListBox Items


3.1. Remove An Item From The ListBox
'Remove 4th item in ListBox (subtract 1 from desired row)
  ListBox1.RemoveItem 3
3.2. Remove Selected Item(s) From The ListBox
Private Sub DeleteSelection()
'PURPOSE: Remove any selected items from the ListBox
Dim x As Long
Dim OriginalCount As Long

'Store original ListBox count


  OriginalCount = ListBox1.ListCount

'Temporarily hide ListBox (runs faster)


  ListBox1.Visible = False

'Delete selected line items


  For x = OriginalCount - 1 To 0 Step -1
    If ListBox1.Selected(x) = True Then ListBox1.RemoveItem x
  Next x

'Unhide ListBox
  ListBox1.Visible = True

End Sub

3.3. Remove All Items From The ListBox


ListBox1.Clear

4. Listbox Selected Items


4.1. Select Blank Option in The ListBox
ListBox1.ListIndex = -1

4.2. Select First Item in The ListBox from the available list
ListBox1.ListIndex = 0

4.3. Select Second Item In The ListBox from the available list
ListBox1.ListIndex = 1

4.4. Select the Last Item in the ListBox from the available list
ListBox1.ListIndex = ListBox1.ListCount - 1

4.5. Select A Specific Item In The ListBox


'Select the 5th item in the ListBox (subtract 1 from desired row)
ListBox1.Selected(4) = True

4.6. Deselect All Items


Unfortunately, the “ListIndex = -1” method does not work when a ListBox allows for multiple selections. Hence, the below code
tests for the ListBox’s selection mode.
If ListBox1.MultiSelect = fmMultiSelectSingle Then
  ListBox1.ListIndex = -1
Else
  For x = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(x) Then ListBox1.Selected(x) = False
  Next x
End If

4.7. Count How Many Items Are Selected (Function)


Function ListBoxSelectionCount(LB As ListBox) As Long
'PURPOSE: Count how many items are selected in a give Listbox

Dim x As Long
Dim Count As Long

For x = 0 To ListBox1.ListCount - 1
  If ListBox1.Selected(x) Then Count = Count + 1
Next x

ListBoxSelectionCount = Count

End Function
5. Count How Many Items Are In The ListBox
'Return how many items are in the ListBox
  MsgBox ListBox1.ListCount

6. Move Selected Item Up/Down


6.1. Move Selection Up One Position
Sub MoveUp()
'PURPOSE: Move the selected item up one position in the list

Dim x As Long
Dim Count As Long
Dim Position As Long

'Is there an item selected?


  If ListBox1.ListIndex = -1 Then Exit Sub

'Which Item is selected?


  For x = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(x) = True Then
      Position = x
      Count = Count + 1
      If Count > 1 Then Exit Sub 'More than 1 item selected
    End If
  Next x

'Selected item already at the top?


  If Position = 0 Then Exit Sub

'Add an item above the current selection


  ListBox1.AddItem ListBox1.List(Position), Position - 1

'Remove Original Selection


  ListBox1.RemoveItem Position + 1

'Re-select the item that got moved


  ListBox1.Selected(Position - 1) = True

End Sub

6.2. Move Selection Down One Position


Sub MoveDown()
'PURPOSE: Move the selected item down one position in the list
Dim x As Long
Dim Count As Long
Dim Position As Long

'Is A ValidSelection Made?


  If ListBox1.ListIndex = -1 Then Exit Sub 'No Selection made

'Which Item is selected?


  For x = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(x) = True Then
      Position = x
      Count = Count + 1
      If Count > 1 Then Exit Sub 'More than 1 item selected
    End If
  Next x

'Move selected item down if not already at the bottom


  If Position < ListBox1.ListCount - 1 Then
    'Add an item below the current selection
      ListBox1.AddItem ListBox1.List(Position), Position + 2
    'Remove Original Selection
      ListBox1.RemoveItem Position
    'Re-select the item that got moved
      ListBox1.Selected(Position + 1) = True
  End If

End Sub

7. Add dynamic ListBox


Private Sub UserForm_Initialize()
Call Add_Dynamic_Listbox
End Sub
Procedure to call
Sub Add_Dynamic_Listbox()
'Add Dynamic List Box and assign it to object 'LstBx'
Set LstBx = Controls.Add("Forms.ListBox.1")

'List Box Position


LstBx.Left = 20
LstBx.Top = 10
End Sub

8. Move all Items from ListBox1 to ListBox2


Sub Move_ListBox_Items()
'Variable declaration
Dim iCnt As Integer

'Moving ListBox1 Items to ListBox2


For iCnt = 0 To ListBox1.ListCount - 1
ListBox2.AddItem ListBox1.List(iCnt)
Next iCnt

'Then Clear the ListBox1 Items


ListBox1.Clear
End Sub

9. Get Selected Items from ListBox1 to ListBox2


Sub Get_ListBox_Selected_Items()
'Variable declaration
Dim iCnt As Integer

'Get Selcted Items from ListBox1 to ListBox2


For iCnt = 0 To ListBox1.ListCount - 1
'Check ListBox Item has selcted or not
If ListBox1.Selected(iCnt) = True Then
ListBox2.AddItem ListBox1.List(iCnt)
End If
Next
End Sub

10. Make ListBox to Select Multiple Items


ListBox1.MultiSelect = fmMultiSelectMulti

11. Remove Duplicates in ListBox


https://analysistabs.com/vba/remove-duplicates-listbox-excel/

You might also like