UserForm Listbox in VBC

M

Marcia3641

Hi:
I added a listbox to my form, but I can't figure out how to add the list of
items.
 
D

dominicb

Good evening Marcia3641

You need to put some code such as this behind the userform:

Private Sub UserForm_Initialize()
With ListBox1
..AddItem "Item 1"
..AddItem "Item 2"
..AddItem "Item 3"
..AddItem "Item 4"
End With
End Sub

This code assumes that your listbox is called ListBox1.

HTH

DominicB
 
M

Marcia3641

Thanks Bob and dominicb. Here is what I have:
Private Sub ListBoxAgeGrp_Click()
..AddItem "Under 25"
..AddItem "26 - 34"
..AddItem "35 - 42"
..AddItem "43 - 55"
..AddItem "56 - 62"
..AddItems "63 and over"
End With
End Sub
Does it make a difference that mine says "click" instead of "initialize"?
Also, how do I check and see if it worked. I tried going to run usferform but
it didn't show the list that I created. Boy this is hard stuff!
 
D

dominicb

Hi Marcia

It does make a difference in that the "Initialize" bit will d
something to the userform when it first opens up, so the Listbox wil
be populated immediately, whereas yours will not populate until yo
click on the button you have named ListBoxAgegrp. Also, the comman
AddItem should only be preceded by a single ".", not two as show
below.

To run your userform set up a small macro such as:

Sub Test()
Userform1.Show
End Sub

and then call the macro, which will run your userform.

Finally, do stick with it! It does seem really baffling at first bu
it does get easier. If you really need a hand try considering buying
book (http://j-walk.com/ss/books/index.htm) to explain the whole concep
in detail. The satisfaction of finishing a project is worth it...!

Good luck

Dominic
 
C

Chip Pearson

The Click event in your code will run only when the list box is
clicked. The list will be populated at that time. In the code
that dominicb wrote, the list box will be populated when the
userform is initialized, generally when it is shown. Use his code
rather that yours.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


message
 
M

Marcia3641

Here is the code I have for my listbox.

Private Sub ListBoxLoanType_Click()
With ListBoxLoanType
..AddItem 'Fixed'
..AddItem 'Interest Only'
..AddItem 'ARM'
End With
End Sub


When I changed it to "initialize" I kept getting a message that it was
ambiguious and then the drop down object box at the top of the code screen
kept changing back to "general" instead of "ListBoxLoanType". Then when I go
to test the form, I am unable to click on the list boxes that I have on the
form. I can tab through all the boxes that are text but I am having no luck
with the three boxes that are list-boxes. Does anything look wrong to you? I
am at my wits end, because this is the only item I am having trouble with on
my form.
 
Top