ListBox Utilities
This page describes code for manipulating list boxes.
The ListBox control is one of the most commonly used controls when developing userforms for
your application. Unfortunately, the listbox itself provides only minimal support for working
with and manipulating the contents of the list. This page describes a number of VBA procedures
you can use to provide more functionality for your users.
All of the procedures described on this page reside in a single module named modListBoxUtils which
you can download here. The various procedures call one another, so you need to
import the entire module to your project rather than just copying and pasting individual procedures. You can download
a demonstration workbook here.
The following section lists and describes the methods available in the modListBoxUtils module. Additional documentation
can be found in the code module.
LBXSelectCount
Public Function LBXSelectCount(LBX As MSForms.ListBox) As Long
This function returns the number of items that are selected in the listbox referenced by LBX.
LBXSelectedIndexes
Public Function LBXSelectedIndexes(LBX As MSForms.ListBox) As Long()
This returns an array of Long values each of which is the index of a selected item in the listbox referenced by LBX.
LBXSwapItems
Public Sub LBXSwapItems(LBX As MSForms.ListBox, N1 As Long, N2 As Long)
This swaps the items in positions N1 and N2 in the listbox referenced by LBX.
LBXInvertSelection
Public Sub LBXInvertSelection(LBX As MSForms.ListBox)
This inverts the selections in the listbox referenced by LBX. Items that are selected
are unselected, and unselected items are selected.
LBXMoveToTop
Public Sub LBXMoveToTop(LBX As MSForms.ListBox)
This moves the selected item to the top of the list in the listbox referenced by LBX.
LBXMoveUp
Public Sub LBXMoveUp(LBX As MSForms.ListBox)
This moves the selected item up one row in the list in the listbox referenced by LBX.
LBXMoveDown
Public Sub LBXMoveDown(LBX As MSForms.ListBox)
This moves the selected item down one row in the list in the listbox referenced by LBX.
LBXMoveToEnd
Public Sub LBXMoveToEnd(LBX As MSForms.ListBox)
This moves the selected item to the end of the list in the listbox referenced by LBX.
LBXSort
Public Sub LBXSort(LBX As MSForms.ListBox, Optional FirstIndex As Long = -1, _
Optional LastIndex As Long = -1, Optional Descending As Boolean = False, _
Optional SelectedItemsOnly As Boolean = False)
This sorts the items in the list of the listbox referenced by LBX.
LBXSelectionInfo
Public Sub LBXSelectionInfo(LBX As MSForms.ListBox, ByRef SelectedCount As Long, _
ByRef FirstSelectedItemIndex As Long, ByRef LastSelectedItemIndex As Long)
This provides information about the listbox referenced by LBX.
LBXSelectedItems
Public Function LBXSelectedItems(LBX As MSForms.ListBox) As String()
This returns an array of strings, each of which is a selected item in the listbox referenced by LBX.
LBXUnSelectAllItems
Public Sub LBXUnSelectAllItems(LBX As MSForms.ListBox)
This unselects all items in the listbox referenced by LBX.
LBXSelectAllItems
Public Sub LBXSelectAllItems(LBX As MSForms.ListBox)
This selects all items in the listbox referenced by LBX.
LBXIsSelectionContiguous
Public Function LBXIsSelectionContiguous(LBX As MSForms.ListBox) As Boolean
This returns True or False indicating whether the selected items in the listbox are contiguous.
|
This page last updated: 28-Oct-2008. |