' Variable to hold array of selected items, when user clicks ' the OK button. Private p_astrItems() As String Private Sub cmdAdd_Click() Dim lngCount As Long ' Add selected items to lstSelected, and remove from ' lstAvailable. With Me.lstAvailable For lngCount = (.ListCount - 1) To 0 Step -1 If .Selected(lngCount) Then Me.lstSelected.AddItem .Column(0, lngCount) .RemoveItem lngCount End If Next End With ' Sort lstSelected. SortListBox Me.lstSelected End Sub Private Sub cmdCancel_Click() Unload Me End Sub Private Sub cmdOK_Click() ' Fill array with items in lstSelected. Dim lngListCount As Long Dim lngCount As Long ' Get list count. lngListCount = Me.lstSelected.ListCount ' Resize array. ReDim p_astrItems(0 To lngListCount - 1) ' Add elements to array. For lngCount = 0 To lngListCount - 1 p_astrItems(lngCount) = Me.lstSelected.Column(0, lngCount) Next ' Add code to work with data in array here.... ' . ' . ' . End Sub Private Sub cmdRemove_Click() Dim lngCount As Long ' Remove selected items from lstSelected, and add to ' lstAvailable. With Me.lstSelected For lngCount = (.ListCount - 1) To 0 Step -1 If .Selected(lngCount) Then Me.lstAvailable.AddItem .Column(0, lngCount) .RemoveItem lngCount End If Next End With ' Sort list box. SortListBox Me.lstAvailable End Sub Private Sub lstAvailable_DblClick(ByVal Cancel As MSForms.ReturnBoolean) cmdAdd_Click End Sub Private Sub lstSelected_Click() cmdRemove_Click End Sub Private Sub UserForm_Initialize() ' Size form to display all controls. Me.Height = cmdOK.Top + cmdOK.Height + 36 Me.Width = lstSelected.Left + lstSelected.Width + 48 ' Add items here.... With Me.lstAvailable ' .AddItem ... ' .AddItem ... ' .AddItem ... ' .AddItem ... ' .AddItem ... End With ' Sort list box. SortListBox Me.lstAvailable End Sub Function SortListBox(lstList As MSForms.ListBox) ' Sort items in single-column list box. The procedure ' adds the items in the list box to an array, sorts the ' array, clears the list box, and adds the items back ' sorted alphabetically. Dim astrListItems() As String Dim lngCount As Long ' Resize array. ReDim astrListItems(0 To lstList.ListCount - 1) ' Fill array with items from list box. For lngCount = 0 To lstList.ListCount - 1 astrListItems(lngCount) = lstList.Column(0, lngCount) Next ' Sort array. QuickSortArray astrListItems ' Clear list and add sorted items from array. With lstList .Clear For lngCount = LBound(astrListItems) To UBound(astrListItems) .AddItem astrListItems(lngCount) Next End With End Function Function QuickSortArray(varArray As Variant, _ Optional lngFirst As Long = -1, _ Optional lngLast As Long = -1) As Variant ' QuickSort algorithm used to sort array. Dim lngLow As Long Dim lngHigh As Long Dim lngMiddle As Long Dim varTempVal As Variant Dim varTestVal As Variant If lngFirst = -1 Then lngFirst = LBound(varArray) If lngLast = -1 Then lngLast = UBound(varArray) If lngFirst < lngLast Then lngMiddle = (lngFirst + lngLast) / 2 varTestVal = varArray(lngMiddle) lngLow = lngFirst lngHigh = lngLast Do Do While varArray(lngLow) < varTestVal lngLow = lngLow + 1 Loop Do While varArray(lngHigh) > varTestVal lngHigh = lngHigh - 1 Loop If (lngLow <= lngHigh) Then varTempVal = varArray(lngLow) varArray(lngLow) = varArray(lngHigh) varArray(lngHigh) = varTempVal lngLow = lngLow + 1 lngHigh = lngHigh - 1 End If Loop While (lngLow <= lngHigh) If lngFirst < lngHigh Then QuickSortArray varArray, lngFirst, lngHigh If lngLow < lngLast Then QuickSortArray varArray, lngLow, lngLast End If End Function