Consulting

Results 1 to 11 of 11

Thread: Solved: Search 2nd ListBox Column

  1. #1
    VBAX Regular
    Joined
    May 2007
    Posts
    81
    Location

    Solved: Search 2nd ListBox Column

    Gurus,

    What am I missing in the following code in order to search the 2nd column of a listbox

    [VBA]
    Private Sub TextBox2_Change()
    Dim Search As String
    Dim n As Long
    Dim i As Long
    Dim j As Long

    Search = Me.TextBox2.Text
    n = Len(Me.TextBox2)
    j = Me.ListBox1.ListCount - 1
    For i = 0 To j
    If Left(Me.ListBox1.List(i), n) = Search Then
    Me.ListBox1.Selected(i) = True
    Exit For
    End If
    Next
    End Sub
    [/VBA]
    Kindest Regards,
    Mike

    I love to Excel

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    add

    Me.TextBox1.BoundColumn = 2
    somewhere. It will cause .List to be pulled from column 2.
    Enter Bound Column in the VBE help for more details, its fairly situation dependent.

  3. #3
    VBAX Regular
    Joined
    May 2007
    Posts
    81
    Location
    I assumed you meant

    [VBA]
    Me.ListBox1.BoundColumn = 2
    [/VBA]

    However, I then need to change my listbox properties to match, in particular my .RowSource range

    Whilst this does the job, sort of, I think I need to redesign to a filter type form as I may have multiple items with the same name and that's all I want to see in the listbox

    Your thoughts....
    Kindest Regards,
    Mike

    I love to Excel

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I don't think you need to reset any of the properties of the ListBox other than .BoundColumn.

  5. #5
    VBAX Regular
    Joined
    May 2007
    Posts
    81
    Location
    Bound Column stays where it is

    .RowSource from A2 to B2, otherwise the listbox displays the same and the search doesn't function
    Kindest Regards,
    Mike

    I love to Excel

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Have you looked at .TextColumn?
    Just as .BoundColumn determines where .Value and .List get their values,
    .TextColumn determines where .Text gets its value.

    Setting .TextColumn and .BoundColumn to different columns might help you look at different parts of your list.

  7. #7
    VBAX Regular
    Joined
    May 2007
    Posts
    81
    Location
    I thought .TextColumn could only be True or False
    Kindest Regards,
    Mike

    I love to Excel

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The help system seems to imply that it will take a column value. The Object Browser lists it as type Variant not Boolean.

  9. #9
    VBAX Regular
    Joined
    May 2007
    Posts
    81
    Location
    It excepted .TextColumn = 2,

    but the search didn't work
    Kindest Regards,
    Mike

    I love to Excel

  10. #10
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    When you substitute a MsgBox for the If statement, does it loop through the entrys you want to check?

  11. #11
    VBAX Regular
    Joined
    May 2007
    Posts
    81
    Location
    I can't send an attachment from this PC.


    When I get to the other one later tonight I'll send through the workbook
    - It's a modification of the address.xls file Lucas sent -


    It'll be a few hours before I can get to it
    Kindest Regards,
    Mike

    I love to Excel

Posting Permissions

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