Consulting

Results 1 to 20 of 20

Thread: Select first Item in Listbox

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Select first Item in Listbox

    I am trying to select the first item in a list box with its index number. This is what I have tried and it keeps crashing:

    [vba]Me.lbColumn1SC.ListIndex.Value = 1[/vba]

    The reason why I am trying it this way is because the list is always different so I can't select in by name.

    I even tried:

    [vba]Me.lbColumn1SC.ListIndex = 1[/vba]

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Djblois --

    Try

    [VBA]

    Private Sub UserForm_Initialize()
    With Me.ListBox1
    .Value = .List(0)
    End With
    End Sub

    [/VBA]

    Paul

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If the list box is on a Userform or from the Toolbox,
    Me.ListBox1.Selected(0) = True
    If its from the Forms menu, its 1 based and this syntax is needed
    ActiveSheet.Shapes("List Box 1").OLEFormat.Object.Selected(1) = True

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    This Crashes the program:

    [VBA]
    Me.ListBox1.Selected(0) = True
    [/VBA]

    and this one won't compile:

    [VBA]With Me.ListBox1
    .Value = .List(0)
    End With[/VBA]

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Can you tell us where the listbox is actually located?

    When are you actually trying to do this?

    Is the listbox actually populated when you try it?

    How is it 'crashing'?

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Why are you making us guess Daniel? Please provide an example...
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Sorry,

    It is in a form. It is populated already. I add the data to it before I show the form with this:

    [VBA]PivotSummary.lbColumn1SC.List = Array("Product", "Division", "Department", "Country", _
    "P-Category", "P-Manager")[/VBA]

    However, I am trying to add this in the forms code window:

    [VBA]With Me.ListBox1
    .Value = .List(0)
    End With[/VBA]

    The reason I am trying to add it there is I use the same form for multiple reports. Ex: Customer Reports and Product Reports and even though I use this to chose one of the items in the list before I show the form, nothing is selected:

    [VBA]PivotSummary.lbColumn1SC.Value = "Product"[/VBA]

    so I am trying to get it selected other ways.

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Daniel

    So how is it 'crashing'?

    What error message if any are you getting?

  9. #9
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    "Could not select the selected Property. Invalid property value."

    and to double check I bypassed the line and it was populated.

    However, if I change the True to False then it doesn't crash. I also tried a value of 1 and it still crashes with the same error message.

  10. #10
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Did you try this?
    [vba]
    Me.lbColumn1SC.ListIndex = 0
    [/vba]

  11. #11
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    no but I tried

    [VBA]me.lbcolumn1SC.listindex = 1[/VBA]

    and that also crashed with a error saying:

    Could not select the set the List Index Property. Invalid property value

  12. #12
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Daniel

    Is listindex definitely in lower case?

  13. #13
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    No, I just checked it is "ListIndex"

  14. #14
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    since you don't seem to be willing to post an example....Paul's method works so you're doing something different which we can't see because......I just don't understand.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #15
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Lucas,

    the underlying code is so large and ingrained in the program it would take me about 3 hours to post an example. I am not trying to make it any harder, just to show what I am doing fully would take a long time.

  16. #16
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I don't know what to say.....did you try the example that I posted with Paul's code? Did it work for you in the example....if so why can't you incorporate it into your code?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  17. #17
    VBAX Newbie
    Joined
    Nov 2007
    Posts
    1
    Location
    Thank you... I needed this!

  18. #18
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Daniel,
    You do have the
    me.lbcolumn1SC.listindex = 0
    line after the line that populates the listbox, don't you? (If not, you will get the error you describe)
    Regards,
    Rory

    Microsoft MVP - Excel

  19. #19
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    4
    Location
    My experience with ListBox controls in UserForms is that setting .ListIndex = n or .Selected(n) = True doesn't seem to help at all. The only workaround that I've found is the following, where "n" is the list item to be selected/highlighted:

        Dim SaveVal As String
        With MyUserForm.SomeListBox
            SaveVal = .List(n)
            .Value = ""
            .Value = SaveVal
            .SetFocus   'Not required, but allows up/down arrow keys to function normally.
        End With
    Note that just re-assigning the selected list item to its current value doesn't work because the underlying code apparently checks for that case and treats it as a no-op.

  20. #20
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    10 yo thread
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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