PDA

View Full Version : Adding item in listbox



volabos
12-03-2007, 05:31 AM
Can anyone here tell me what is the current way to add items in listbox?

I have a userform, named userform1 and there, I created a listbox named listbox1.

In the code window for userform1, wrote following codes:

Sub ShowUserForm1()
'Fill the list box
With UserForm1.ListBox1
.RowSource = ""
.AddItem "January"
.AddItem "February"
.AddItem "March"
End With
UserForm1.Show
End Sub

However when I open the listbox, those items are not displaying. Am I making any mistake?

Regards,

Bob Phillips
12-03-2007, 06:01 AM
Works here. Post an example.

unmarkedhelicopter
12-03-2007, 06:03 AM
Show the form first then in the initialise code, populate the controls.
Is your other thread solved ? Was the last answer acceptable ?
Please let us know otherwise more people will devote time to it, perhaps unecessarily.

volabos
12-03-2007, 06:30 AM
Hi unmarkedhelicopter, can you give me an example, how to do that? If you give me a step-by-step approach it would be fine

Regards,

volabos
12-03-2007, 06:35 AM
Hi all, at last I got the solution. I have changed my code as follows:

Private Sub CommandButton1_Click()
UserForm1.Show
With UserForm1.ListBox1
.RowSource = ""
.AddItem "January"
.AddItem "February"
.AddItem "March"
End With
End Sub

Thanks everyone for having look on my problem.
:friends:

unmarkedhelicopter
12-03-2007, 06:47 AM
No, I didn't mean like that.
If you call your form from more than one source you will have to write that code out each time.
When you have a form you can write code that actually resides with that form, in VBE scoll to form, click on it and then view code, the initialise event code should then open for you, write it in there.
In the form you can also refer to the form as "me" so :-
userform1.listbox1 becomes me.listbox1 that way your code won't fall over if you rename your form.

Yeah that works fine ...

Bob Phillips
12-03-2007, 06:57 AM
I would still like to know why your original code didn't work, as it should have. The later code has redundancy, as you are trying to show it when it will already be shown from the module code.

It is not necessary to show the form first and then populate the controls as suggested earlier. I generally populate controls outside of the form.

Can you post tat original non-working example?

volabos
12-03-2007, 07:27 AM
Hi Xld, even I am also surprised why my previous code was not working. Actually in my excel file itself, I have a command button. When I click on this button, the user form should be displayed. In this userform, the list box is there. This is my basic plan. However regaeding the original code, i have already displayed here. If you need any further information please let me know.

Regards,

Bob Phillips
12-03-2007, 07:34 AM
As I said, I would like to see the original non-working workbook.

Norie
12-03-2007, 07:46 AM
volabos

Normally when populating a listbox/combobox the code would go in the userform's initialize event and if you were using AddItem you shouldn't be using RowSource.


Private Sub UserForm_Initialize()
Dim I As Long
For I = 1 To 12
ListBox1.AddItem MonthName(I)
Next I
End Sub