30.04.2017 Views

4523756273

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 18: UserForm Techniques and Tricks<br />

299<br />

If no item in the ListBox is selected, the ListIndex property returns –1.<br />

However, this will never be the case for this particular ListBox, because code<br />

in the UserForm_Initialize procedure selected the first item. So, there will<br />

always be a selected item, if the user doesn’t actually select a month.<br />

Figure 18-4 shows how this looks.<br />

Figure 18-4:<br />

Determining<br />

which item<br />

in a ListBox<br />

is selected.<br />

The first item in a ListBox has a ListIndex of 0, not 1 (as you might expect).<br />

This is always the case, even if you use an Option Base 1 statement to change<br />

the default lower bound for arrays.<br />

This example is available at this book’s Web site.<br />

Determining multiple selections<br />

If your ListBox is set up so the user can select more than one item, you find<br />

that the ListIndex property returns only the last item selected. To determine<br />

all selected items, you need to use the Selected property, which contains an<br />

array.<br />

To allow multiple selections in a ListBox, set the MultiSelect property to either<br />

1 or 2. You can do so at design time by using the Properties window or at runtime<br />

by using a VBA statement such as this:<br />

UserForm1.ListBox1.MultiSelect = 1<br />

The MultiSelect property has three possible settings. The meaning of each is<br />

shown in Table 18-1.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!