Advanced controls in userforms with VBA macros
Part five of a nine-part series of blogs

The two previous parts of this mini-blog have shown how to draw forms and how to write code to handle form events. This final part shows how to add some of the more exotic controls to user forms, like combo boxes, list boxes, multipage controls, spinners and option buttons.

  1. Advanced Controls - Our Example
  2. Multipage Controls
  3. Combo boxes (and list boxes)
  4. Multiple Column Combo Boxes and Listboxes
  5. Multi-select ListBoxes (this blog)
  6. SpinButtons (Spinners)
  7. Check Boxes
  8. Option Buttons (Radio Buttons)
  9. The Calendar Control

This blog is part of our Excel VBA tutorial.  Wise Owl's main business is running Excel, VBA and other courses for businesses.

Posted by Andy Brown on 28 February 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Multi-select ListBoxes

The previous two parts of this blog have considered general features for combo and list boxes, but there is one thing you can do with a listbox that you can't do with a combo box: allow a user to select lots of records at the same time.

Making a ListBox Multi-Select

This is the easy part!  Just change the properties of the listbox as follows:

Three possible Multiselect properties

You can choose 3 possible values for a MultiSelect listbox.  The difference between frmMultiSelectMulti and frmMultiSelectExtended (who thinks up these names?) is that with the latter you can click and drag to select lots of adjacent items at the same time.

 

You can then click on any item to select (or deselect) it:

Selecting multiple items

Here we have selected at least 3 departments (more may be shown selected if we scroll down).

 

Picking Up on Items Selected in Code

You can loop over the items in the Selected array to find out which items were chosen:

Private Sub btnOrder_Click()

Dim EachDepartment As Long

'loop over all of the items in the list

For EachDepartment = 1 To Me.lstDepartment.ListCount

'for each item, see if it was selected

If Me.lstDepartment.Selected(EachDepartment - 1) = True Then

'do something with it!

Debug.Print Me.lstDepartment.List(EachDepartment - 1)

End If

Next EachDepartment

End Sub

Notice here that the ListCount property gives the number of items in the list (numbered from 1), but the Selected array is numbered from 0.

 

This blog has 0 threads Add post