Consulting

Results 1 to 10 of 10

Thread: Adding item in listbox

  1. #1

    Adding item in listbox

    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,

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Works here. Post an example.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    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.
    2+2=9 ... (My Arithmetic Is Mental)

  4. #4
    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,

  5. #5
    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.

  6. #6
    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 ...
    2+2=9 ... (My Arithmetic Is Mental)

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    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,

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As I said, I would like to see the original non-working workbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •