Excel VBA Change List Box Multi Select Property on Toggle

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Excel VBA Change List Box Multi Select Property on Toggle

Post by MSingh »

Hi,

Using Toggle Button1, how can i change List Box1 property from multiselect to singleselection?

What is the benefit of populating a listbox at runtime as opposed to it being pre-populated using a dynamic range/named range?

Thanking you in advance.
Mohamed

User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA Change List Box Multi Select Property on Toggle

Post by HansV »

You can use code like this to switch between single select and multi select:

Code: Select all

Private Sub ToggleButton1_Click()
  If Me.ToggleButton1 Then
    Me.ListBox1.MultiSelect = fmMultiSelectMulti
  Else
    Me.ListBox1.MultiSelect = fmMultiSelectSingle
  End If
End Sub
Please keep in mind that changing the MultiSelect property will cancel the current selection, i.e. after clicking the toggle button, nothing will be selected in the list box.

If you populate a listbox at runtime using AddItem or by setting the List property, you can completely control the contents of the list box. You can later remove individual items using RemoveItem. This is not possible with a list box bound to a range of cells (you'd have to delete one or more cells to remove an item).

The advantages of populating a list box from a range of cells by setting the RowSource are:
1) You don't have to write code to fill the list box, and
2) You can set the ColumnHeads property to Yes; the column headings will automatically be taken from the row above the RowSource. ColumnHeads don't work if you fill the list box using code.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Excel VBA Change List Box Multi Select Property on Toggle

Post by MSingh »

Hi,

Thanks again for that very quick answer that works first time.

My selection in Listbox1 with frmMatchEntryComplete always appears at the bottom of the List, sometimes below the frame. How can i correct this?

Many many Thanks
Mohamed

User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA Change List Box Multi Select Property on Toggle

Post by HansV »

Make sure that the IntegralHeight property of the list box is set to True. If IntegralHeight is True and the problem still persists, the problem will be hard to solve - you might experiment with the height of the list box; perhaps the problem goes away if you make the list box slightly taller or less tall.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Excel VBA Change List Box Multi Select Property on Toggle

Post by MSingh »

Hi,

IntegralHeight=True did the job.

Kind Regards
Mohamed