List Boxes in UserForms

B

blaglobal

Hi all,

I am new to VBA and I am trying to complete a userform in Word - using
the 'insert userform command'. I have added a list box to the
userform but I can't type in the choices I want it to show. Does
anyone have any ideas please?
Thanks,
blaglobal
 
J

Jay Freedman

blaglobal said:
Hi all,

I am new to VBA and I am trying to complete a userform in Word - using
the 'insert userform command'. I have added a list box to the
userform but I can't type in the choices I want it to show. Does
anyone have any ideas please?
Thanks,
blaglobal

The general idea is to use code in either the Userform_Initialize or the
Userform_Activate procedure to load items into the list box. There are a
couple of ways to go about this.

One consideration is where the text of the items comes from. They can be
constant -- the same for all runs of the userform -- and stored directly in
the code or somewhere in the template (e.g., as AutoText or as document
variables). The simplest case would be

Private Sub UserForm_Activate()
With ListBox1
.AddItem "one"
.AddItem "two"
.AddItem "three"
End With
End Sub

An alternative is to make a variable of type Variant, store the items in it
as an array, and assign the variable to the listbox's .List property. This
is much easier to do than to describe:

Private Sub UserForm_Activate()
Dim temp As Variant
temp = Array("one", "two", "three")
ListBox1.List = temp
End Sub

When you start to deal with larger amounts of data, or with data that may
change from one run to another, you need to use other storage methods and
ways of reading the data. For example, you might store the items in a text
file, which you read with the Open and Line Input commands. They might be in
an Excel worksheet or an Access database, which offer things like ADO
(Active Data Objects) and ODBC (Open Data Base Connectivity) -- ways of
directly connecting the listbox to the data source. But that's a story for
another week...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top