'Module: comForm 'Purpose: Common form handling procedures 'VB Version: Excel 97 SR-1 'Written by: David Crawford ' Option Explicit ' 'Purpose: Returns first selected item from a listbox 'Examples: getSelectedString([]) -> "Blue" ' Function GetSelectedString(ListBox As Object) As String On Error GoTo HandleErrors Dim ItemArray As Variant Dim ItemCount As Integer Dim ItemIndex As Integer 'assume no items are selected GetSelectedString = "" 'get array of items ItemArray = ListBox.Selected 'find the first selected item in array ItemCount = UBound(ItemArray) For ItemIndex = 1 To ItemCount If ItemArray(ItemIndex) Then GetSelectedString = ListBox.List(ItemIndex) Exit For End If Next ItemIndex Exit Function HandleErrors: Exit Function End Function ' 'Purpose: ' Function IsItemSelected( _ ListBox As Object, _ ByVal itemString As String) As Boolean Dim ItemArray As Variant Dim ItemIndex As Integer Dim ItemCount As Integer 'make itemString upper case so match is not case sensitive itemString = UCase$(itemString) 'for each item in list box ItemArray = ListBox.Selected ItemCount = UBound(ItemArray) For ItemIndex = 1 To ItemCount 'if item is selected If ItemArray(ItemIndex) = True Then 'if item matches then return true If UCase$(ListBox.List(ItemIndex)) = itemString Then IsItemSelected = True Exit Function End If End If Next ItemIndex 'no match found return false IsItemSelected = False End Function ' 'Purpose: Return true if any item in a list box is selected ' Function IsAnyItemSelected(ListBox As Object) As Boolean Dim ItemArray As Variant Dim ItemIndex As Integer Dim ItemCount As Integer 'for each item in list box ItemCount = ListBox.ListCount For ItemIndex = 0 To ItemCount - 1 'if item is selected return true If ListBox.Selected(ItemIndex) = True Then IsAnyItemSelected = True Exit Function End If Next ItemIndex 'no match found return false IsAnyItemSelected = False End Function