VBA Listbox

This is a text and video example of how to implement a VBA Listbox:

On your Excel worksheet show your “Forms Toolbar”. In Excel 2003 and prior, right click the standard toolbar (at the top of your screen) and choose “Forms Toolbar”. In Excel 2007 and above click the round Microsoft Office Button at the top left or your screen. Then choose “Excel Options” at the bottom of the list. The choose “Popular” and select the “Show Developer tab…” and select to show the developer tab.

Add list box to your worksheet, then choose to show the properties. When you right click the listbox control, choose “Properties” and “Control”, then key in “A1” for the cell link. Call one of your worksheets “data”, and for “InputRange” enter “data!A1:A5” (that’s the naming structure [worksheet name]![cell range]). Make sure you enter some values into the sheet called “data” and cells “A1-A5” or else you won’t show anything in your new combobox.
This is what your worksheet should look like:
Cell A1 = “red”
Cell A2 = “blue”
Cell A3 = “yellow”
Cell A4 = “green”
Cell A5 = “brown”
It’s going to look like this, and you’ll have to apply the principles to your situation:
************************************************
For the single selection option

Private Sub SelectSingleValue()

   Dim intCount As Integer
   Dim strTemp As String

   strTemp = lstColors.Value
   Range("A1") = strTemp

End Sub

***********************************************
***********************************************
For the multiple selection option

Private Sub SelectMultiValues()

   Dim intCount As Integer
   Dim strTemp As String
   Dim lngCellCount As Integer

   lngCellCount = 1

   For intCount = 0 To lstColors.ListCount - 1
      If lstColors.Selected(intCount) = True Then
         strTemp = lstColors.List(intCount)
         Range("A" & lngCellCount) = strTemp

         lngCellCount = lngCellCount + 1
      End If
   Next

End Sub

*********************************************************
Now watch how it’s done (both the single and multiple select)…

Let me know if you have any questions

[simple_contact_form]


Posted

in

by

Tags: