Results 1 to 3 of 3

Thread: A magic listbox

  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A magic listbox

    Okay, this one is for the die-hards.
    I am strugling with it for a time and it still doesn't work like I want.

    I have a multi-selection listbox on a form with 4 columns, the first one having width 0, the others visible.

    Before showing the form I want to preselect a couple of rows in the listbox, based on a criterium known in the code.

    The selection is on one of the columns in the listbox (the second visible, so the third column in the listbox itself)

    Example:

    row column1 column2 column3
    1 aa 2 bb
    2 bb 1 cc
    3 dd 2 gg
    4 ee 4 aa

    I want to search the rows in the listbox on the value of column2 and preselect if this is 2, so rows 1 and 3 should be preselected.

    What does the VBA code to do such a thing look like?

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: A magic listbox

    Hi Bart,
    Something like this should do it:
    Private Sub Form_Load()
    Dim intRow As Integer
    With Me!List0
    For intRow = 0 To .ListCount - 1
    If .Column(2, intRow) = 2 Then
    .Selected(intRow) = True
    End If
    Next 'introw
    End With
    End Sub
    You can change the = 2 to whatever your criteria are.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: A magic listbox

    Thanks Rory, you gave me the solution.
    I didn't read the invaluable information under the F1 button completely and overlooked the second parameter on the Column property.

Posting Permissions

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